14#ifndef LLVM_CODEGEN_GLOBALISEL_UTILS_H
15#define LLVM_CODEGEN_GLOBALISEL_UTILS_H
32class LostDebugLocObserver;
33class MachineBasicBlock;
34class BlockFrequencyInfo;
35class GISelValueTracking;
38class MachineIRBuilder;
40class MachineOptimizationRemarkEmitter;
41class MachineOptimizationRemarkMissed;
42struct MachinePointerInfo;
43class MachineRegisterInfo;
45class ProfileSummaryInfo;
46class RegisterBankInfo;
49class TargetPassConfig;
50class TargetRegisterInfo;
51class TargetRegisterClass;
56#define GISEL_VECREDUCE_CASES_ALL \
57 case TargetOpcode::G_VECREDUCE_SEQ_FADD: \
58 case TargetOpcode::G_VECREDUCE_SEQ_FMUL: \
59 case TargetOpcode::G_VECREDUCE_FADD: \
60 case TargetOpcode::G_VECREDUCE_FMUL: \
61 case TargetOpcode::G_VECREDUCE_FMAX: \
62 case TargetOpcode::G_VECREDUCE_FMIN: \
63 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
64 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
65 case TargetOpcode::G_VECREDUCE_ADD: \
66 case TargetOpcode::G_VECREDUCE_MUL: \
67 case TargetOpcode::G_VECREDUCE_AND: \
68 case TargetOpcode::G_VECREDUCE_OR: \
69 case TargetOpcode::G_VECREDUCE_XOR: \
70 case TargetOpcode::G_VECREDUCE_SMAX: \
71 case TargetOpcode::G_VECREDUCE_SMIN: \
72 case TargetOpcode::G_VECREDUCE_UMAX: \
73 case TargetOpcode::G_VECREDUCE_UMIN:
75#define GISEL_VECREDUCE_CASES_NONSEQ \
76 case TargetOpcode::G_VECREDUCE_FADD: \
77 case TargetOpcode::G_VECREDUCE_FMUL: \
78 case TargetOpcode::G_VECREDUCE_FMAX: \
79 case TargetOpcode::G_VECREDUCE_FMIN: \
80 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
81 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
82 case TargetOpcode::G_VECREDUCE_ADD: \
83 case TargetOpcode::G_VECREDUCE_MUL: \
84 case TargetOpcode::G_VECREDUCE_AND: \
85 case TargetOpcode::G_VECREDUCE_OR: \
86 case TargetOpcode::G_VECREDUCE_XOR: \
87 case TargetOpcode::G_VECREDUCE_SMAX: \
88 case TargetOpcode::G_VECREDUCE_SMIN: \
89 case TargetOpcode::G_VECREDUCE_UMAX: \
90 case TargetOpcode::G_VECREDUCE_UMIN:
97 const TargetInstrInfo &
TII,
99 const TargetRegisterClass &RegClass);
110 const MachineFunction &MF,
const TargetRegisterInfo &
TRI,
111 MachineRegisterInfo &
MRI,
const TargetInstrInfo &
TII,
112 const RegisterBankInfo &RBI, MachineInstr &InsertPt,
113 const TargetRegisterClass &RegClass, MachineOperand &RegMO);
126 const MachineFunction &MF,
const TargetRegisterInfo &
TRI,
127 MachineRegisterInfo &
MRI,
const TargetInstrInfo &
TII,
128 const RegisterBankInfo &RBI, MachineInstr &InsertPt,
const MCInstrDesc &
II,
129 MachineOperand &RegMO,
unsigned OpIdx);
141 const TargetInstrInfo &
TII,
142 const TargetRegisterInfo &
TRI,
143 const RegisterBankInfo &RBI);
148 MachineRegisterInfo &
MRI);
153 const MachineRegisterInfo &
MRI);
158 const TargetPassConfig &TPC,
159 MachineOptimizationRemarkEmitter &
MORE,
160 MachineOptimizationRemarkMissed &R);
163 const TargetPassConfig &TPC,
164 MachineOptimizationRemarkEmitter &
MORE,
165 const char *
PassName, StringRef Msg,
166 const MachineInstr &
MI);
171 const TargetPassConfig &TPC,
172 MachineOptimizationRemarkEmitter &
MORE,
173 MachineOptimizationRemarkMissed &R);
189 const MachineRegisterInfo &
MRI);
203 bool LookThroughInstrs =
true);
209 bool LookThroughInstrs =
true,
bool LookThroughAnyExt =
false);
218LLVM_ABI std::optional<FPValueAndVReg>
221 bool LookThroughInstrs =
true);
243LLVM_ABI std::optional<DefinitionAndSourceRegister>
290 return dyn_cast_or_null<T>(
DefMI);
303 const MachineRegisterInfo &
MRI);
306 const MachineRegisterInfo &
MRI);
312 const MachineRegisterInfo &
MRI);
316 const MachineRegisterInfo &
MRI);
321 const MachineRegisterInfo &
MRI);
325 const MachineRegisterInfo &
MRI);
330LLVM_ABI std::optional<SmallVector<unsigned>>
332 std::function<
unsigned(APInt)> CB);
334LLVM_ABI std::optional<SmallVector<APInt>>
336 unsigned DstScalarSizeInBits,
unsigned ExtOp,
337 const MachineRegisterInfo &
MRI);
344 GISelValueTracking *ValueTracking =
nullptr);
357 const MachinePointerInfo &MPO);
366 MachineFunction &MF,
const TargetInstrInfo &
TII, MCRegister PhysReg,
367 const TargetRegisterClass &RC,
const DebugLoc &
DL, LLT RegTy = LLT());
415 bool isReg()
const {
return IsReg; }
416 bool isCst()
const {
return !IsReg; }
448 const MachineRegisterInfo &
MRI);
452LLVM_ABI std::optional<FPValueAndVReg>
454 bool AllowUndef =
true);
459 const MachineRegisterInfo &
MRI,
460 int64_t SplatValue,
bool AllowUndef);
465 const MachineRegisterInfo &
MRI,
466 APInt SplatValue,
bool AllowUndef);
471 const MachineRegisterInfo &
MRI,
472 int64_t SplatValue,
bool AllowUndef);
477 const MachineRegisterInfo &
MRI,
478 APInt SplatValue,
bool AllowUndef);
483 const MachineRegisterInfo &
MRI,
484 bool AllowUndef =
false);
489 const MachineRegisterInfo &
MRI,
490 bool AllowUndef =
false);
499 const MachineRegisterInfo &
MRI,
501 bool AllowOpaqueConstants =
true);
508 const MachineRegisterInfo &
MRI,
509 bool AllowUndefs =
false);
514 const MachineRegisterInfo &
MRI,
515 bool AllowUndefs =
false);
536LLVM_ABI std::optional<RegOrConstant>
542 const MachineRegisterInfo &
MRI);
549 const MachineRegisterInfo &
MRI);
556 const MachineRegisterInfo &
MRI);
563 std::function<
bool(
const Constant *ConstVal)> Match,
564 bool AllowUndefs =
false);
569 bool IsVector,
bool IsFP);
573 bool IsVector,
bool IsFP);
607 bool ConsiderFlagsAndMetadata =
true);
611 bool ConsiderFlagsAndMetadata =
true);
671 LLVM_ABI static std::optional<GIConstant>
712 "Expected fixed vector or scalar constant");
713 return Values.
begin();
718 "Expected fixed vector or scalar constant");
724 return Values.
size();
730 LLVM_ABI static std::optional<GFConstant>
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
Implement a low-level type suitable for MachineInstr level instruction selection.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
static const char PassName[]
Class for arbitrary precision integers.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ConstantFP - Floating Point Values [float, double].
An floating-point-like constant.
const_iterator begin() const
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
GFConstant(const APFloat &Value, GFConstantKind Kind)
GFConstant(ArrayRef< APFloat > Values)
GFConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
LLVM_ABI APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
const_iterator end() const
An integer-like constant.
LLVM_ABI APInt getScalarValue() const
Returns the value, if this constant is a scalar.
GIConstant(const APInt &Value, GIConstantKind Kind)
static LLVM_ABI std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
GIConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
GIConstant(ArrayRef< APInt > Values)
This is an important class for using LLVM in a threaded context.
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Represents a value which can be a Register or a constant.
RegOrConstant(Register Reg)
RegOrConstant(int64_t Cst)
Wrapper class representing virtual and physical registers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Register getFunctionLiveInPhysReg(MachineFunction &MF, const TargetInstrInfo &TII, MCRegister PhysReg, const TargetRegisterClass &RC, const DebugLoc &DL, LLT RegTy=LLT())
Return a virtual register corresponding to the incoming argument register PhysReg.
LLVM_ABI std::optional< SmallVector< APInt > > ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2, unsigned DstScalarSizeInBits, unsigned ExtOp, const MachineRegisterInfo &MRI)
LLVM_ABI bool isBuildVectorAllZeros(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndef=false)
Return true if the specified instruction is a G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all of the...
LLVM_ABI Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
LLVM_ABI Register constrainOperandRegClass(const MachineFunction &MF, const TargetRegisterInfo &TRI, MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, MachineInstr &InsertPt, const TargetRegisterClass &RegClass, MachineOperand &RegMO)
Constrain the Register operand OpIdx, so that it is now constrained to the TargetRegisterClass passed...
LLVM_ABI MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
LLVM_ABI const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
LLVM_ABI std::optional< APFloat > ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
LLVM_ABI std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
LLVM_ABI std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
LLVM_ABI bool constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
LLVM_ABI std::optional< SmallVector< unsigned > > ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI, std::function< unsigned(APInt)> CB)
Tries to constant fold a counting-zero operation (G_CTLZ or G_CTTZ) on Src.
LLVM_ABI std::optional< APInt > ConstantFoldExtOp(unsigned Opcode, const Register Op1, uint64_t Imm, const MachineRegisterInfo &MRI)
LLVM_ABI std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a constant integer or a splat vector of constant integers.
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
LLVM_ABI bool matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg, std::function< bool(const Constant *ConstVal)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant G_B...
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI bool isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Returns true if given the TargetLowering's boolean contents information, the value Val contains a tru...
LLVM_ABI LLVM_READNONE LLT getLCMType(LLT OrigTy, LLT TargetTy)
Return the least common multiple type of OrigTy and TargetTy, by changing the number of vector elemen...
LLVM_ABI std::optional< int64_t > getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
LLVM_ABI std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
LLVM_ABI const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
LLVM_ABI bool isConstantOrConstantVector(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowFP=true, bool AllowOpaqueConstants=true)
Return true if the specified instruction is known to be a constant, or a vector of constants.
LLVM_ABI bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
LLVM_ABI void saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver, SmallInstListTy &DeadInstChain)
LLVM_ABI void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream.
LLVM_ABI std::optional< ValueAndVReg > getAnyConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true, bool LookThroughAnyExt=false)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT or G_FCONST...
LLVM_ABI bool isBuildVectorAllOnes(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndef=false)
Return true if the specified instruction is a G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all of the...
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI SmallVector< APInt > ConstantFoldVectorBinop(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Tries to constant fold a vector binop with sources Op1 and Op2.
LLVM_ABI std::optional< FPValueAndVReg > getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI, bool AllowUndef=true)
Returns a floating point scalar constant of a build vector splat if it exists.
LLVM_ABI std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
LLVM_ABI void extractParts(Register Reg, LLT Ty, int NumParts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Helper function to split a wide generic register into bitwise blocks with the given Type (which impli...
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
LLVM_ABI LLVM_READNONE LLT getCoverTy(LLT OrigTy, LLT TargetTy)
Return smallest type that covers both OrigTy and TargetTy and is multiple of TargetTy.
LLVM_ABI unsigned getInverseGMinMaxOpcode(unsigned MinMaxOpc)
Returns the inverse opcode of MinMaxOpc, which is a generic min/max opcode like G_SMIN.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
LLVM_ABI std::optional< FPValueAndVReg > getFConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_FCONSTANT returns it...
LLVM_ABI bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
LLVM_ABI std::optional< APFloat > isConstantOrConstantSplatVectorFP(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a float constant integer or a splat vector of float constant integers.
LLVM_ABI APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
LLVM_ABI bool isBuildVectorConstantSplat(const Register Reg, const MachineRegisterInfo &MRI, int64_t SplatValue, bool AllowUndef)
Return true if the specified register is defined by G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all ...
LLVM_ABI void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
LLVM_ABI Register constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, Register Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
LLVM_ABI bool isPreISelGenericFloatingPointOpcode(unsigned Opc)
Returns whether opcode Opc is a pre-isel generic floating-point opcode, having only floating-point op...
bool isKnownNeverSNaN(Register Val, const MachineRegisterInfo &MRI)
Returns true if Val can be assumed to never be a signaling NaN.
LLVM_ABI std::optional< DefinitionAndSourceRegister > getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, and underlying value Register folding away any copies.
LLVM_ABI void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI Register getSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the source register for Reg, folding away any trivial copies.
LLVM_ABI LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy)
Return a type where the total size is the greatest common divisor of OrigTy and TargetTy.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI std::optional< int64_t > getIConstantSplatSExtVal(const Register Reg, const MachineRegisterInfo &MRI)
LLVM_ABI bool isAssertMI(const MachineInstr &MI)
Returns true if the instruction MI is one of the assert instructions.
LLVM_ABI void extractVectorParts(Register Reg, unsigned NumElts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Version which handles irregular sub-vector splits.
LLVM_ABI int getSplatIndex(ArrayRef< int > Mask)
If all non-negative Mask elements are the same value, return that value.
LLVM_ABI bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn't have oth...
LLVM_ABI Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
LLVM_ABI void reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel warning as a missed optimization remark to the LLVMContext's diagnostic stream.
Simple struct used to hold a Register value and the instruction which defines it.
Simple struct used to hold a constant integer value and a virtual register.