14#ifndef LLVM_CODEGEN_GLOBALISEL_UTILS_H
15#define LLVM_CODEGEN_GLOBALISEL_UTILS_H
31class LostDebugLocObserver;
32class MachineBasicBlock;
33class BlockFrequencyInfo;
37class MachineIRBuilder;
39class MachineOptimizationRemarkEmitter;
40class MachineOptimizationRemarkMissed;
41struct MachinePointerInfo;
42class MachineRegisterInfo;
44class ProfileSummaryInfo;
45class RegisterBankInfo;
48class TargetPassConfig;
49class TargetRegisterInfo;
50class TargetRegisterClass;
55#define GISEL_VECREDUCE_CASES_ALL \
56 case TargetOpcode::G_VECREDUCE_SEQ_FADD: \
57 case TargetOpcode::G_VECREDUCE_SEQ_FMUL: \
58 case TargetOpcode::G_VECREDUCE_FADD: \
59 case TargetOpcode::G_VECREDUCE_FMUL: \
60 case TargetOpcode::G_VECREDUCE_FMAX: \
61 case TargetOpcode::G_VECREDUCE_FMIN: \
62 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
63 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
64 case TargetOpcode::G_VECREDUCE_ADD: \
65 case TargetOpcode::G_VECREDUCE_MUL: \
66 case TargetOpcode::G_VECREDUCE_AND: \
67 case TargetOpcode::G_VECREDUCE_OR: \
68 case TargetOpcode::G_VECREDUCE_XOR: \
69 case TargetOpcode::G_VECREDUCE_SMAX: \
70 case TargetOpcode::G_VECREDUCE_SMIN: \
71 case TargetOpcode::G_VECREDUCE_UMAX: \
72 case TargetOpcode::G_VECREDUCE_UMIN:
74#define GISEL_VECREDUCE_CASES_NONSEQ \
75 case TargetOpcode::G_VECREDUCE_FADD: \
76 case TargetOpcode::G_VECREDUCE_FMUL: \
77 case TargetOpcode::G_VECREDUCE_FMAX: \
78 case TargetOpcode::G_VECREDUCE_FMIN: \
79 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
80 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
81 case TargetOpcode::G_VECREDUCE_ADD: \
82 case TargetOpcode::G_VECREDUCE_MUL: \
83 case TargetOpcode::G_VECREDUCE_AND: \
84 case TargetOpcode::G_VECREDUCE_OR: \
85 case TargetOpcode::G_VECREDUCE_XOR: \
86 case TargetOpcode::G_VECREDUCE_SMAX: \
87 case TargetOpcode::G_VECREDUCE_SMIN: \
88 case TargetOpcode::G_VECREDUCE_UMAX: \
89 case TargetOpcode::G_VECREDUCE_UMIN:
96 const TargetInstrInfo &
TII,
98 const TargetRegisterClass &RegClass);
109 const TargetRegisterInfo &
TRI,
110 MachineRegisterInfo &
MRI,
111 const TargetInstrInfo &
TII,
112 const RegisterBankInfo &RBI,
113 MachineInstr &InsertPt,
114 const TargetRegisterClass &RegClass,
115 MachineOperand &RegMO);
128 const TargetRegisterInfo &
TRI,
129 MachineRegisterInfo &
MRI,
130 const TargetInstrInfo &
TII,
131 const RegisterBankInfo &RBI,
132 MachineInstr &InsertPt,
const MCInstrDesc &
II,
133 MachineOperand &RegMO,
unsigned OpIdx);
145 const TargetInstrInfo &
TII,
146 const TargetRegisterInfo &
TRI,
147 const RegisterBankInfo &RBI);
160 MachineOptimizationRemarkEmitter &
MORE,
161 MachineOptimizationRemarkMissed &R);
164 MachineOptimizationRemarkEmitter &
MORE,
165 const char *
PassName, StringRef Msg,
166 const MachineInstr &
MI);
171 MachineOptimizationRemarkEmitter &
MORE,
172 MachineOptimizationRemarkMissed &R);
176 const MachineRegisterInfo &
MRI);
180 const MachineRegisterInfo &
MRI);
194std::optional<ValueAndVReg>
197 bool LookThroughInstrs =
true);
203 bool LookThroughInstrs =
true,
bool LookThroughAnyExt =
false);
212std::optional<FPValueAndVReg>
215 bool LookThroughInstrs =
true);
237std::optional<DefinitionAndSourceRegister>
280 return dyn_cast_or_null<T>(
DefMI);
292 const MachineRegisterInfo &
MRI);
295 const MachineRegisterInfo &
MRI);
301 const MachineRegisterInfo &
MRI);
305 const MachineRegisterInfo &
MRI);
309 const MachineRegisterInfo &
MRI);
313 const MachineRegisterInfo &
MRI);
318std::optional<SmallVector<unsigned>>
320 std::function<
unsigned(APInt)> CB);
322std::optional<SmallVector<APInt>>
324 const MachineRegisterInfo &
MRI);
330 GISelKnownBits *KnownBits =
nullptr);
351 const TargetInstrInfo &
TII,
353 const TargetRegisterClass &RC,
354 const DebugLoc &
DL, LLT RegTy = LLT());
403 bool isReg()
const {
return IsReg; }
404 bool isCst()
const {
return !IsReg; }
421 const MachineRegisterInfo &
MRI);
425 const MachineRegisterInfo &
MRI);
430 const MachineRegisterInfo &
MRI);
435 const MachineRegisterInfo &
MRI);
440 const MachineRegisterInfo &
MRI,
441 bool AllowUndef =
true);
446 const MachineRegisterInfo &
MRI,
447 int64_t SplatValue,
bool AllowUndef);
452 const MachineRegisterInfo &
MRI,
453 int64_t SplatValue,
bool AllowUndef);
458 const MachineRegisterInfo &
MRI,
459 bool AllowUndef =
false);
464 const MachineRegisterInfo &
MRI,
465 bool AllowUndef =
false);
474 const MachineRegisterInfo &
MRI,
476 bool AllowOpaqueConstants =
true);
483 bool AllowUndefs =
false);
488 const MachineRegisterInfo &
MRI,
489 bool AllowUndefs =
false);
511 const MachineRegisterInfo &
MRI);
516 const MachineRegisterInfo &
MRI);
523 const MachineRegisterInfo &
MRI);
529 std::function<
bool(
const Constant *ConstVal)>
Match,
530 bool AllowUndefs =
false);
534bool isConstTrueVal(
const TargetLowering &TLI, int64_t Val,
bool IsVector,
538bool isConstFalseVal(
const TargetLowering &TLI, int64_t Val,
bool IsVector,
543int64_t
getICmpTrueVal(
const TargetLowering &TLI,
bool IsVector,
bool IsFP);
569 bool ConsiderFlagsAndMetadata =
true);
573 bool ConsiderFlagsAndMetadata =
true);
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
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.
unsigned const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
static 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.
APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
An integer-like constant.
APInt getScalarValue() const
Returns the value, if this constant is a scalar.
GIConstant(const APInt &Value, GIConstantKind Kind)
static 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.
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.
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...
Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
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...
MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
std::optional< APFloat > ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
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...
std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
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...
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...
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.
std::optional< APInt > ConstantFoldExtOp(unsigned Opcode, const Register Op1, uint64_t Imm, const MachineRegisterInfo &MRI)
std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
std::optional< APInt > isConstantOrConstantSplatVector(MachineInstr &MI, const MachineRegisterInfo &MRI)
Determines if MI defines a constant integer or a splat vector of constant integers.
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...
MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
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...
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.
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_READNONE LLT getLCMType(LLT OrigTy, LLT TargetTy)
Return the least common multiple type of OrigTy and TargetTy, by changing the number of vector elemen...
std::optional< int64_t > getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
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.
bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
void saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver, SmallInstListTy &DeadInstChain)
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.
std::optional< SmallVector< APInt > > ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
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...
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...
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
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.
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.
std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
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...
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
LLVM_READNONE LLT getCoverTy(LLT OrigTy, LLT TargetTy)
Return smallest type that covers both OrigTy and TargetTy and is multiple of TargetTy.
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.
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...
bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
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 ...
void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Register constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, Register Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
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...
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.
std::optional< DefinitionAndSourceRegister > getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, and underlying value Register folding away any copies.
void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
bool isKnownNeverNaN(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
Register getSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the source register for Reg, folding away any trivial copies.
LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy)
Return a type where the total size is the greatest common divisor of OrigTy and TargetTy.
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.
std::optional< int64_t > getIConstantSplatSExtVal(const Register Reg, const MachineRegisterInfo &MRI)
void extractVectorParts(Register Reg, unsigned NumElts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Version which handles irregular sub-vector splits.
int getSplatIndex(ArrayRef< int > Mask)
If all non-negative Mask elements are the same value, return that value.
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...
Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
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.