LLVM 20.0.0git
Utils.h
Go to the documentation of this file.
1//==-- llvm/CodeGen/GlobalISel/Utils.h ---------------------------*- C++ -*-==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file This file declares the API of helper functions used throughout the
10/// GlobalISel pipeline.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_GLOBALISEL_UTILS_H
15#define LLVM_CODEGEN_GLOBALISEL_UTILS_H
16
17#include "GISelWorkList.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/DebugLoc.h"
25
26#include <cstdint>
27
28namespace llvm {
29
30class AnalysisUsage;
31class LostDebugLocObserver;
32class MachineBasicBlock;
33class BlockFrequencyInfo;
34class GISelKnownBits;
35class MachineFunction;
36class MachineInstr;
37class MachineIRBuilder;
38class MachineOperand;
39class MachineOptimizationRemarkEmitter;
40class MachineOptimizationRemarkMissed;
41struct MachinePointerInfo;
42class MachineRegisterInfo;
43class MCInstrDesc;
44class ProfileSummaryInfo;
45class RegisterBankInfo;
46class TargetInstrInfo;
47class TargetLowering;
48class TargetPassConfig;
49class TargetRegisterInfo;
50class TargetRegisterClass;
51class ConstantFP;
52class APFloat;
53
54// Convenience macros for dealing with vector reduction opcodes.
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:
73
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:
90
91/// Try to constrain Reg to the specified register class. If this fails,
92/// create a new virtual register in the correct class.
93///
94/// \return The virtual register constrained to the right register class.
95Register constrainRegToClass(MachineRegisterInfo &MRI,
96 const TargetInstrInfo &TII,
97 const RegisterBankInfo &RBI, Register Reg,
98 const TargetRegisterClass &RegClass);
99
100/// Constrain the Register operand OpIdx, so that it is now constrained to the
101/// TargetRegisterClass passed as an argument (RegClass).
102/// If this fails, create a new virtual register in the correct class and insert
103/// a COPY before \p InsertPt if it is a use or after if it is a definition.
104/// In both cases, the function also updates the register of RegMo. The debug
105/// location of \p InsertPt is used for the new copy.
106///
107/// \return The virtual register constrained to the right register class.
108Register constrainOperandRegClass(const MachineFunction &MF,
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);
116
117/// Try to constrain Reg so that it is usable by argument OpIdx of the provided
118/// MCInstrDesc \p II. If this fails, create a new virtual register in the
119/// correct class and insert a COPY before \p InsertPt if it is a use or after
120/// if it is a definition. In both cases, the function also updates the register
121/// of RegMo.
122/// This is equivalent to constrainOperandRegClass(..., RegClass, ...)
123/// with RegClass obtained from the MCInstrDesc. The debug location of \p
124/// InsertPt is used for the new copy.
125///
126/// \return The virtual register constrained to the right register class.
127Register constrainOperandRegClass(const MachineFunction &MF,
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);
134
135/// Mutate the newly-selected instruction \p I to constrain its (possibly
136/// generic) virtual register operands to the instruction's register class.
137/// This could involve inserting COPYs before (for uses) or after (for defs).
138/// This requires the number of operands to match the instruction description.
139/// \returns whether operand regclass constraining succeeded.
140///
141// FIXME: Not all instructions have the same number of operands. We should
142// probably expose a constrain helper per operand and let the target selector
143// constrain individual registers, like fast-isel.
144bool constrainSelectedInstRegOperands(MachineInstr &I,
145 const TargetInstrInfo &TII,
146 const TargetRegisterInfo &TRI,
147 const RegisterBankInfo &RBI);
148
149/// Check if DstReg can be replaced with SrcReg depending on the register
150/// constraints.
151bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI);
152
153/// Check whether an instruction \p MI is dead: it only defines dead virtual
154/// registers, and doesn't have other side effects.
155bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI);
156
157/// Report an ISel error as a missed optimization remark to the LLVMContext's
158/// diagnostic stream. Set the FailedISel MachineFunction property.
159void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
160 MachineOptimizationRemarkEmitter &MORE,
161 MachineOptimizationRemarkMissed &R);
162
163void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC,
164 MachineOptimizationRemarkEmitter &MORE,
165 const char *PassName, StringRef Msg,
166 const MachineInstr &MI);
167
168/// Report an ISel warning as a missed optimization remark to the LLVMContext's
169/// diagnostic stream.
170void reportGISelWarning(MachineFunction &MF, const TargetPassConfig &TPC,
171 MachineOptimizationRemarkEmitter &MORE,
172 MachineOptimizationRemarkMissed &R);
173
174/// If \p VReg is defined by a G_CONSTANT, return the corresponding value.
175std::optional<APInt> getIConstantVRegVal(Register VReg,
176 const MachineRegisterInfo &MRI);
177
178/// If \p VReg is defined by a G_CONSTANT fits in int64_t returns it.
179std::optional<int64_t> getIConstantVRegSExtVal(Register VReg,
180 const MachineRegisterInfo &MRI);
181
182/// \p VReg is defined by a G_CONSTANT, return the corresponding value.
183const APInt &getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI);
184
185/// Simple struct used to hold a constant integer value and a virtual
186/// register.
190};
191
192/// If \p VReg is defined by a statically evaluable chain of instructions rooted
193/// on a G_CONSTANT returns its APInt value and def register.
194std::optional<ValueAndVReg>
197 bool LookThroughInstrs = true);
198
199/// If \p VReg is defined by a statically evaluable chain of instructions rooted
200/// on a G_CONSTANT or G_FCONSTANT returns its value as APInt and def register.
201std::optional<ValueAndVReg> getAnyConstantVRegValWithLookThrough(
202 Register VReg, const MachineRegisterInfo &MRI,
203 bool LookThroughInstrs = true, bool LookThroughAnyExt = false);
204
208};
209
210/// If \p VReg is defined by a statically evaluable chain of instructions rooted
211/// on a G_FCONSTANT returns its APFloat value and def register.
212std::optional<FPValueAndVReg>
215 bool LookThroughInstrs = true);
216
218 const MachineRegisterInfo &MRI);
219
220/// See if Reg is defined by an single def instruction that is
221/// Opcode. Also try to do trivial folding if it's a COPY with
222/// same types. Returns null otherwise.
223MachineInstr *getOpcodeDef(unsigned Opcode, Register Reg,
224 const MachineRegisterInfo &MRI);
225
226/// Simple struct used to hold a Register value and the instruction which
227/// defines it.
231};
232
233/// Find the def instruction for \p Reg, and underlying value Register folding
234/// away any copies.
235///
236/// Also walks through hints such as G_ASSERT_ZEXT.
237std::optional<DefinitionAndSourceRegister>
239
240/// Find the def instruction for \p Reg, folding away any trivial copies. May
241/// return nullptr if \p Reg is not a generic virtual register.
242///
243/// Also walks through hints such as G_ASSERT_ZEXT.
245 const MachineRegisterInfo &MRI);
246
247/// Find the source register for \p Reg, folding away any trivial copies. It
248/// will be an output register of the instruction that getDefIgnoringCopies
249/// returns. May return an invalid register if \p Reg is not a generic virtual
250/// register.
251///
252/// Also walks through hints such as G_ASSERT_ZEXT.
254
255/// Helper function to split a wide generic register into bitwise blocks with
256/// the given Type (which implies the number of blocks needed). The generic
257/// registers created are appended to Ops, starting at bit 0 of Reg.
258void extractParts(Register Reg, LLT Ty, int NumParts,
261
262/// Version which handles irregular splits.
263bool extractParts(Register Reg, LLT RegTy, LLT MainTy, LLT &LeftoverTy,
265 SmallVectorImpl<Register> &LeftoverVRegs,
267
268/// Version which handles irregular sub-vector splits.
269void extractVectorParts(Register Reg, unsigned NumElts,
272
273// Templated variant of getOpcodeDef returning a MachineInstr derived T.
274/// See if Reg is defined by an single def instruction of type T
275/// Also try to do trivial folding if it's a COPY with
276/// same types. Returns null otherwise.
277template <class T>
280 return dyn_cast_or_null<T>(DefMI);
281}
282
283/// Returns an APFloat from Val converted to the appropriate size.
284APFloat getAPFloatFromSize(double Val, unsigned Size);
285
286/// Modify analysis usage so it preserves passes required for the SelectionDAG
287/// fallback.
288void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU);
289
290std::optional<APInt> ConstantFoldBinOp(unsigned Opcode, const Register Op1,
291 const Register Op2,
292 const MachineRegisterInfo &MRI);
293std::optional<APFloat> ConstantFoldFPBinOp(unsigned Opcode, const Register Op1,
294 const Register Op2,
295 const MachineRegisterInfo &MRI);
296
297/// Tries to constant fold a vector binop with sources \p Op1 and \p Op2.
298/// Returns an empty vector on failure.
299SmallVector<APInt> ConstantFoldVectorBinop(unsigned Opcode, const Register Op1,
300 const Register Op2,
301 const MachineRegisterInfo &MRI);
302
303std::optional<APInt> ConstantFoldCastOp(unsigned Opcode, LLT DstTy,
304 const Register Op0,
305 const MachineRegisterInfo &MRI);
306
307std::optional<APInt> ConstantFoldExtOp(unsigned Opcode, const Register Op1,
308 uint64_t Imm,
309 const MachineRegisterInfo &MRI);
310
311std::optional<APFloat> ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy,
312 Register Src,
313 const MachineRegisterInfo &MRI);
314
315/// Tries to constant fold a counting-zero operation (G_CTLZ or G_CTTZ) on \p
316/// Src. If \p Src is a vector then it tries to do an element-wise constant
317/// fold.
318std::optional<SmallVector<unsigned>>
319ConstantFoldCountZeros(Register Src, const MachineRegisterInfo &MRI,
320 std::function<unsigned(APInt)> CB);
321
322std::optional<SmallVector<APInt>>
323ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2,
324 const MachineRegisterInfo &MRI);
325
326/// Test if the given value is known to have exactly one bit set. This differs
327/// from computeKnownBits in that it doesn't necessarily determine which bit is
328/// set.
329bool isKnownToBeAPowerOfTwo(Register Val, const MachineRegisterInfo &MRI,
330 GISelKnownBits *KnownBits = nullptr);
331
332/// Returns true if \p Val can be assumed to never be a NaN. If \p SNaN is true,
333/// this returns if \p Val can be assumed to never be a signaling NaN.
334bool isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
335 bool SNaN = false);
336
337/// Returns true if \p Val can be assumed to never be a signaling NaN.
339 return isKnownNeverNaN(Val, MRI, true);
340}
341
342Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO);
343
344/// Return a virtual register corresponding to the incoming argument register \p
345/// PhysReg. This register is expected to have class \p RC, and optional type \p
346/// RegTy. This assumes all references to the register will use the same type.
347///
348/// If there is an existing live-in argument register, it will be returned.
349/// This will also ensure there is a valid copy
350Register getFunctionLiveInPhysReg(MachineFunction &MF,
351 const TargetInstrInfo &TII,
352 MCRegister PhysReg,
353 const TargetRegisterClass &RC,
354 const DebugLoc &DL, LLT RegTy = LLT());
355
356/// Return the least common multiple type of \p OrigTy and \p TargetTy, by
357/// changing the number of vector elements or scalar bitwidth. The intent is a
358/// G_MERGE_VALUES, G_BUILD_VECTOR, or G_CONCAT_VECTORS can be constructed from
359/// \p OrigTy elements, and unmerged into \p TargetTy. It is an error to call
360/// this function where one argument is a fixed vector and the other is a
361/// scalable vector, since it is illegal to build a G_{MERGE|UNMERGE}_VALUES
362/// between fixed and scalable vectors.
364LLT getLCMType(LLT OrigTy, LLT TargetTy);
365
367/// Return smallest type that covers both \p OrigTy and \p TargetTy and is
368/// multiple of TargetTy.
369LLT getCoverTy(LLT OrigTy, LLT TargetTy);
370
371/// Return a type where the total size is the greatest common divisor of \p
372/// OrigTy and \p TargetTy. This will try to either change the number of vector
373/// elements, or bitwidth of scalars. The intent is the result type can be used
374/// as the result of a G_UNMERGE_VALUES from \p OrigTy, and then some
375/// combination of G_MERGE_VALUES, G_BUILD_VECTOR and G_CONCAT_VECTORS (possibly
376/// with intermediate casts) can re-form \p TargetTy.
377///
378/// If these are vectors with different element types, this will try to produce
379/// a vector with a compatible total size, but the element type of \p OrigTy. If
380/// this can't be satisfied, this will produce a scalar smaller than the
381/// original vector elements. It is an error to call this function where
382/// one argument is a fixed vector and the other is a scalable vector, since it
383/// is illegal to build a G_{MERGE|UNMERGE}_VALUES between fixed and scalable
384/// vectors.
385///
386/// In the worst case, this returns LLT::scalar(1)
388LLT getGCDType(LLT OrigTy, LLT TargetTy);
389
390/// Represents a value which can be a Register or a constant.
391///
392/// This is useful in situations where an instruction may have an interesting
393/// register operand or interesting constant operand. For a concrete example,
394/// \see getVectorSplat.
396 int64_t Cst;
397 Register Reg;
398 bool IsReg;
399
400public:
401 explicit RegOrConstant(Register Reg) : Reg(Reg), IsReg(true) {}
402 explicit RegOrConstant(int64_t Cst) : Cst(Cst), IsReg(false) {}
403 bool isReg() const { return IsReg; }
404 bool isCst() const { return !IsReg; }
405 Register getReg() const {
406 assert(isReg() && "Expected a register!");
407 return Reg;
408 }
409 int64_t getCst() const {
410 assert(isCst() && "Expected a constant!");
411 return Cst;
412 }
413};
414
415/// \returns The splat index of a G_SHUFFLE_VECTOR \p MI when \p MI is a splat.
416/// If \p MI is not a splat, returns std::nullopt.
417std::optional<int> getSplatIndex(MachineInstr &MI);
418
419/// \returns the scalar integral splat value of \p Reg if possible.
420std::optional<APInt> getIConstantSplatVal(const Register Reg,
421 const MachineRegisterInfo &MRI);
422
423/// \returns the scalar integral splat value defined by \p MI if possible.
424std::optional<APInt> getIConstantSplatVal(const MachineInstr &MI,
425 const MachineRegisterInfo &MRI);
426
427/// \returns the scalar sign extended integral splat value of \p Reg if
428/// possible.
429std::optional<int64_t> getIConstantSplatSExtVal(const Register Reg,
430 const MachineRegisterInfo &MRI);
431
432/// \returns the scalar sign extended integral splat value defined by \p MI if
433/// possible.
434std::optional<int64_t> getIConstantSplatSExtVal(const MachineInstr &MI,
435 const MachineRegisterInfo &MRI);
436
437/// Returns a floating point scalar constant of a build vector splat if it
438/// exists. When \p AllowUndef == true some elements can be undef but not all.
439std::optional<FPValueAndVReg> getFConstantSplat(Register VReg,
440 const MachineRegisterInfo &MRI,
441 bool AllowUndef = true);
442
443/// Return true if the specified register is defined by G_BUILD_VECTOR or
444/// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
445bool isBuildVectorConstantSplat(const Register Reg,
446 const MachineRegisterInfo &MRI,
447 int64_t SplatValue, bool AllowUndef);
448
449/// Return true if the specified instruction is a G_BUILD_VECTOR or
450/// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
451bool isBuildVectorConstantSplat(const MachineInstr &MI,
452 const MachineRegisterInfo &MRI,
453 int64_t SplatValue, bool AllowUndef);
454
455/// Return true if the specified instruction is a G_BUILD_VECTOR or
456/// G_BUILD_VECTOR_TRUNC where all of the elements are 0 or undef.
457bool isBuildVectorAllZeros(const MachineInstr &MI,
458 const MachineRegisterInfo &MRI,
459 bool AllowUndef = false);
460
461/// Return true if the specified instruction is a G_BUILD_VECTOR or
462/// G_BUILD_VECTOR_TRUNC where all of the elements are ~0 or undef.
463bool isBuildVectorAllOnes(const MachineInstr &MI,
464 const MachineRegisterInfo &MRI,
465 bool AllowUndef = false);
466
467/// Return true if the specified instruction is known to be a constant, or a
468/// vector of constants.
469///
470/// If \p AllowFP is true, this will consider G_FCONSTANT in addition to
471/// G_CONSTANT. If \p AllowOpaqueConstants is true, constant-like instructions
472/// such as G_GLOBAL_VALUE will also be considered.
473bool isConstantOrConstantVector(const MachineInstr &MI,
474 const MachineRegisterInfo &MRI,
475 bool AllowFP = true,
476 bool AllowOpaqueConstants = true);
477
478/// Return true if the value is a constant 0 integer or a splatted vector of a
479/// constant 0 integer (with no undefs if \p AllowUndefs is false). This will
480/// handle G_BUILD_VECTOR and G_BUILD_VECTOR_TRUNC as truncation is not an issue
481/// for null values.
482bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI,
483 bool AllowUndefs = false);
484
485/// Return true if the value is a constant -1 integer or a splatted vector of a
486/// constant -1 integer (with no undefs if \p AllowUndefs is false).
487bool isAllOnesOrAllOnesSplat(const MachineInstr &MI,
488 const MachineRegisterInfo &MRI,
489 bool AllowUndefs = false);
490
491/// \returns a value when \p MI is a vector splat. The splat can be either a
492/// Register or a constant.
493///
494/// Examples:
495///
496/// \code
497/// %reg = COPY $physreg
498/// %reg_splat = G_BUILD_VECTOR %reg, %reg, ..., %reg
499/// \endcode
500///
501/// If called on the G_BUILD_VECTOR above, this will return a RegOrConstant
502/// containing %reg.
503///
504/// \code
505/// %cst = G_CONSTANT iN 4
506/// %constant_splat = G_BUILD_VECTOR %cst, %cst, ..., %cst
507/// \endcode
508///
509/// In the above case, this will return a RegOrConstant containing 4.
510std::optional<RegOrConstant> getVectorSplat(const MachineInstr &MI,
511 const MachineRegisterInfo &MRI);
512
513/// Determines if \p MI defines a constant integer or a build vector of
514/// constant integers. Treats undef values as constants.
515bool isConstantOrConstantVector(MachineInstr &MI,
516 const MachineRegisterInfo &MRI);
517
518/// Determines if \p MI defines a constant integer or a splat vector of
519/// constant integers.
520/// \returns the scalar constant or std::nullopt.
521std::optional<APInt>
523 const MachineRegisterInfo &MRI);
524
525/// Attempt to match a unary predicate against a scalar/splat constant or every
526/// element of a constant G_BUILD_VECTOR. If \p ConstVal is null, the source
527/// value was undef.
528bool matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg,
529 std::function<bool(const Constant *ConstVal)> Match,
530 bool AllowUndefs = false);
531
532/// Returns true if given the TargetLowering's boolean contents information,
533/// the value \p Val contains a true value.
534bool isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
535 bool IsFP);
536/// \returns true if given the TargetLowering's boolean contents information,
537/// the value \p Val contains a false value.
538bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector,
539 bool IsFP);
540
541/// Returns an integer representing true, as defined by the
542/// TargetBooleanContents.
543int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP);
544
547 LostDebugLocObserver *LocObserver,
548 SmallInstListTy &DeadInstChain);
550 LostDebugLocObserver *LocObserver = nullptr);
552 LostDebugLocObserver *LocObserver = nullptr);
553
554/// Assuming the instruction \p MI is going to be deleted, attempt to salvage
555/// debug users of \p MI by writing the effect of \p MI in a DIExpression.
557
558/// Returns whether opcode \p Opc is a pre-isel generic floating-point opcode,
559/// having only floating-point operands.
560bool isPreISelGenericFloatingPointOpcode(unsigned Opc);
561
562/// Returns true if \p Reg can create undef or poison from non-undef &
563/// non-poison operands. \p ConsiderFlagsAndMetadata controls whether poison
564/// producing flags and metadata on the instruction are considered. This can be
565/// used to see if the instruction could still introduce undef or poison even
566/// without poison generating flags and metadata which might be on the
567/// instruction.
569 bool ConsiderFlagsAndMetadata = true);
570
571/// Returns true if \p Reg can create poison from non-poison operands.
573 bool ConsiderFlagsAndMetadata = true);
574
575/// Returns true if \p Reg cannot be poison and undef.
578 unsigned Depth = 0);
579
580/// Returns true if \p Reg cannot be poison, but may be undef.
582 unsigned Depth = 0);
583
584/// Returns true if \p Reg cannot be undef, but may be poison.
586 unsigned Depth = 0);
587
588/// Get the type back from LLT. It won't be 100 percent accurate but returns an
589/// estimate of the type.
591
592/// An integer-like constant.
593///
594/// It abstracts over scalar, fixed-length vectors, and scalable vectors.
595/// In the common case, it provides a common API and feels like an APInt,
596/// while still providing low-level access.
597/// It can be used for constant-folding.
598///
599/// bool isZero()
600/// abstracts over the kind.
601///
602/// switch(const.getKind())
603/// {
604/// }
605/// provides low-level access.
607public:
609
610private:
611 GIConstantKind Kind;
612 SmallVector<APInt> Values;
613 APInt Value;
614
615public:
617 : Kind(GIConstantKind::FixedVector), Values(Values) {};
619 : Kind(Kind), Value(Value) {};
620
621 /// Returns the kind of of this constant, e.g, Scalar.
622 GIConstantKind getKind() const { return Kind; }
623
624 /// Returns the value, if this constant is a scalar.
625 APInt getScalarValue() const;
626
627 static std::optional<GIConstant> getConstant(Register Const,
628 const MachineRegisterInfo &MRI);
629};
630
631/// An floating-point-like constant.
632///
633/// It abstracts over scalar, fixed-length vectors, and scalable vectors.
634/// In the common case, it provides a common API and feels like an APFloat,
635/// while still providing low-level access.
636/// It can be used for constant-folding.
637///
638/// bool isZero()
639/// abstracts over the kind.
640///
641/// switch(const.getKind())
642/// {
643/// }
644/// provides low-level access.
646public:
648
649private:
650 GFConstantKind Kind;
652
653public:
655 : Kind(GFConstantKind::FixedVector), Values(Values) {};
656 GFConstant(const APFloat &Value, GFConstantKind Kind) : Kind(Kind) {
657 Values.push_back(Value);
658 }
659
660 /// Returns the kind of of this constant, e.g, Scalar.
661 GFConstantKind getKind() const { return Kind; }
662
663 /// Returns the value, if this constant is a scalar.
664 APFloat getScalarValue() const;
665
666 static std::optional<GFConstant> getConstant(Register Const,
667 const MachineRegisterInfo &MRI);
668};
669
670} // End namespace llvm.
671#endif
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
basic Basic Alias true
#define LLVM_READNONE
Definition: Compiler.h:299
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Implement a low-level type suitable for MachineInstr level instruction selection.
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
unsigned Reg
Promote Memory to Register
Definition: Mem2Reg.cpp:110
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char PassName[]
Class for arbitrary precision integers.
Definition: APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
An floating-point-like constant.
Definition: Utils.h:645
static std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:2014
GFConstant(const APFloat &Value, GFConstantKind Kind)
Definition: Utils.h:656
GFConstant(ArrayRef< APFloat > Values)
Definition: Utils.h:654
GFConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition: Utils.h:661
APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
Definition: Utils.cpp:2007
An integer-like constant.
Definition: Utils.h:606
APInt getScalarValue() const
Returns the value, if this constant is a scalar.
Definition: Utils.cpp:1967
GIConstant(const APInt &Value, GIConstantKind Kind)
Definition: Utils.h:618
static std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1974
GIConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition: Utils.h:622
GIConstant(ArrayRef< APInt > Values)
Definition: Utils.h:616
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Helper class to build MachineInstr.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Represents a value which can be a Register or a constant.
Definition: Utils.h:395
RegOrConstant(Register Reg)
Definition: Utils.h:401
Register getReg() const
Definition: Utils.h:405
bool isCst() const
Definition: Utils.h:404
int64_t getCst() const
Definition: Utils.h:409
RegOrConstant(int64_t Cst)
Definition: Utils.h:402
bool isReg() const
Definition: Utils.h:403
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ ConstantFP
Definition: ISDOpcodes.h:77
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Definition: Utils.cpp:895
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...
Definition: Utils.cpp:1425
Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
Definition: Utils.cpp:1960
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...
Definition: Utils.cpp:56
MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
Definition: Utils.cpp:630
const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:444
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.
Definition: Utils.cpp:279
std::optional< APFloat > ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:969
std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1385
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...
Definition: Utils.cpp:1538
std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:718
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...
Definition: Utils.cpp:1656
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...
Definition: Utils.cpp:155
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.
Definition: Utils.cpp:982
std::optional< APInt > ConstantFoldExtOp(unsigned Opcode, const Register Op1, uint64_t Imm, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:928
std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1438
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.
Definition: Utils.cpp:1508
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...
Definition: Utils.cpp:1520
MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
Definition: Utils.cpp:471
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...
Definition: Utils.cpp:1553
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...
Definition: Utils.cpp:1585
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...
Definition: Utils.cpp:1157
std::optional< int64_t > getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT fits in int64_t returns it.
Definition: Utils.cpp:299
std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:649
const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
Definition: Utils.cpp:290
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.
Definition: Utils.cpp:1488
bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
Definition: Utils.cpp:201
void saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver, SmallInstListTy &DeadInstChain)
Definition: Utils.cpp:1622
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.
Definition: Utils.cpp:259
std::optional< SmallVector< APInt > > ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:1014
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...
Definition: Utils.cpp:424
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...
Definition: Utils.cpp:1431
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.
Definition: Utils.cpp:772
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.
Definition: Utils.cpp:1418
std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
Definition: Utils.cpp:945
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...
Definition: Utils.cpp:485
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition: Utils.cpp:1153
LLVM_READNONE LLT getCoverTy(LLT OrigTy, LLT TargetTy)
Return smallest type that covers both OrigTy and TargetTy and is multiple of TargetTy.
Definition: Utils.cpp:1224
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...
Definition: Utils.cpp:432
bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Definition: Utils.cpp:1598
APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
Definition: Utils.cpp:636
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 ...
Definition: Utils.cpp:1369
void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition: Utils.cpp:1651
Register constrainRegToClass(MachineRegisterInfo &MRI, const TargetInstrInfo &TII, const RegisterBankInfo &RBI, Register Reg, const TargetRegisterClass &RegClass)
Try to constrain Reg to the specified register class.
Definition: Utils.cpp:46
int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
Definition: Utils.cpp:1610
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...
Definition: Utils.cpp:418
bool isPreISelGenericFloatingPointOpcode(unsigned Opc)
Returns whether opcode Opc is a pre-isel generic floating-point opcode, having only floating-point op...
Definition: Utils.cpp:1675
bool isKnownNeverSNaN(Register Val, const MachineRegisterInfo &MRI)
Returns true if Val can be assumed to never be a signaling NaN.
Definition: Utils.h:338
std::optional< DefinitionAndSourceRegister > getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, and underlying value Register folding away any copies.
Definition: Utils.cpp:452
void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition: Utils.cpp:1636
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.
Definition: Utils.cpp:478
LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy)
Return a type where the total size is the greatest common divisor of OrigTy and TargetTy.
Definition: Utils.cpp:1245
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)
Definition: Utils.cpp:1403
void extractVectorParts(Register Reg, unsigned NumElts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Version which handles irregular sub-vector splits.
Definition: Utils.cpp:588
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...
Definition: Utils.cpp:222
Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition: Utils.cpp:878
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.
Definition: Utils.cpp:253
#define MORE()
Definition: regcomp.c:252
Simple struct used to hold a Register value and the instruction which defines it.
Definition: Utils.h:228
Register VReg
Definition: Utils.h:207
Simple struct used to hold a constant integer value and a virtual register.
Definition: Utils.h:187
Register VReg
Definition: Utils.h:189