LLVM 24.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"
26
27#include <cstdint>
28#include <vector>
29
30namespace llvm {
31
32class AnalysisUsage;
37class MachineFunction;
38class MachineInstr;
40class MachineOperand;
45class MCInstrDesc;
48class TargetInstrInfo;
49class TargetLowering;
52class MCRegisterClass;
54class ConstantFP;
55class APFloat;
56
57// Convenience macros for dealing with vector reduction opcodes.
58#define GISEL_VECREDUCE_CASES_ALL \
59 case TargetOpcode::G_VECREDUCE_SEQ_FADD: \
60 case TargetOpcode::G_VECREDUCE_SEQ_FMUL: \
61 case TargetOpcode::G_VECREDUCE_FADD: \
62 case TargetOpcode::G_VECREDUCE_FMUL: \
63 case TargetOpcode::G_VECREDUCE_FMAX: \
64 case TargetOpcode::G_VECREDUCE_FMIN: \
65 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
66 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
67 case TargetOpcode::G_VECREDUCE_FMAXIMUMNUM: \
68 case TargetOpcode::G_VECREDUCE_FMINIMUMNUM: \
69 case TargetOpcode::G_VECREDUCE_ADD: \
70 case TargetOpcode::G_VECREDUCE_MUL: \
71 case TargetOpcode::G_VECREDUCE_AND: \
72 case TargetOpcode::G_VECREDUCE_OR: \
73 case TargetOpcode::G_VECREDUCE_XOR: \
74 case TargetOpcode::G_VECREDUCE_SMAX: \
75 case TargetOpcode::G_VECREDUCE_SMIN: \
76 case TargetOpcode::G_VECREDUCE_UMAX: \
77 case TargetOpcode::G_VECREDUCE_UMIN:
78
79#define GISEL_VECREDUCE_CASES_NONSEQ \
80 case TargetOpcode::G_VECREDUCE_FADD: \
81 case TargetOpcode::G_VECREDUCE_FMUL: \
82 case TargetOpcode::G_VECREDUCE_FMAX: \
83 case TargetOpcode::G_VECREDUCE_FMIN: \
84 case TargetOpcode::G_VECREDUCE_FMAXIMUM: \
85 case TargetOpcode::G_VECREDUCE_FMINIMUM: \
86 case TargetOpcode::G_VECREDUCE_FMAXIMUMNUM: \
87 case TargetOpcode::G_VECREDUCE_FMINIMUMNUM: \
88 case TargetOpcode::G_VECREDUCE_ADD: \
89 case TargetOpcode::G_VECREDUCE_MUL: \
90 case TargetOpcode::G_VECREDUCE_AND: \
91 case TargetOpcode::G_VECREDUCE_OR: \
92 case TargetOpcode::G_VECREDUCE_XOR: \
93 case TargetOpcode::G_VECREDUCE_SMAX: \
94 case TargetOpcode::G_VECREDUCE_SMIN: \
95 case TargetOpcode::G_VECREDUCE_UMAX: \
96 case TargetOpcode::G_VECREDUCE_UMIN:
97
98/// Try to constrain Reg to the specified register class. If this fails,
99/// create a new virtual register in the correct class.
100///
101/// \return The virtual register constrained to the right register class.
102LLVM_ABI Register constrainRegToClass(MachineRegisterInfo &MRI,
103 const TargetInstrInfo &TII,
104 const RegisterBankInfo &RBI, Register Reg,
105 const TargetRegisterClass &RegClass);
106
107/// Constrain the Register operand OpIdx, so that it is now constrained to the
108/// TargetRegisterClass passed as an argument (RegClass).
109/// If this fails, create a new virtual register in the correct class and insert
110/// a COPY before \p InsertPt if it is a use or after if it is a definition.
111/// In both cases, the function also updates the register of RegMo. The debug
112/// location of \p InsertPt is used for the new copy.
113///
114/// \return The virtual register constrained to the right register class.
116 const MachineFunction &MF, const TargetRegisterInfo &TRI,
117 MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
118 const RegisterBankInfo &RBI, MachineInstr &InsertPt,
119 const TargetRegisterClass &RegClass, MachineOperand &RegMO);
120
121/// Try to constrain Reg so that it is usable by argument OpIdx of the provided
122/// MCInstrDesc \p II. If this fails, create a new virtual register in the
123/// correct class and insert a COPY before \p InsertPt if it is a use or after
124/// if it is a definition. In both cases, the function also updates the register
125/// of RegMo.
126/// This is equivalent to constrainOperandRegClass(..., RegClass, ...)
127/// with RegClass obtained from the MCInstrDesc. The debug location of \p
128/// InsertPt is used for the new copy.
129///
130/// \return The virtual register constrained to the right register class.
132 const MachineFunction &MF, const TargetRegisterInfo &TRI,
133 MachineRegisterInfo &MRI, const TargetInstrInfo &TII,
134 const RegisterBankInfo &RBI, MachineInstr &InsertPt, const MCInstrDesc &II,
135 MachineOperand &RegMO, unsigned OpIdx);
136
137/// Mutate the newly-selected instruction \p I to constrain its (possibly
138/// generic) virtual register operands to the instruction's register class.
139/// This could involve inserting COPYs before (for uses) or after (for defs).
140/// This requires the number of operands to match the instruction description.
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.
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.
151LLVM_ABI bool canReplaceReg(Register DstReg, Register SrcReg,
152 MachineRegisterInfo &MRI);
153
154/// Check whether an instruction \p MI is dead: it only defines dead virtual
155/// registers, and doesn't have other side effects.
156LLVM_ABI bool isTriviallyDead(const MachineInstr &MI,
157 const MachineRegisterInfo &MRI);
158
159/// Report an ISel error as a missed optimization remark to the LLVMContext's
160/// diagnostic stream. Set the FailedISel MachineFunction property.
162 MachineOptimizationRemarkEmitter &MORE,
163 MachineOptimizationRemarkMissed &R);
164
166 MachineOptimizationRemarkEmitter &MORE,
167 const char *PassName, StringRef Msg,
168 const MachineInstr &MI);
169
170/// Report an ISel warning as a missed optimization remark to the LLVMContext's
171/// diagnostic stream.
173 MachineOptimizationRemarkEmitter &MORE,
174 MachineOptimizationRemarkMissed &R);
175
176/// Returns the inverse opcode of \p MinMaxOpc, which is a generic min/max
177/// opcode like G_SMIN.
178LLVM_ABI unsigned getInverseGMinMaxOpcode(unsigned MinMaxOpc);
179
180/// If \p VReg is defined by a G_CONSTANT, return the corresponding value.
181LLVM_ABI std::optional<APInt>
182getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI);
183
184/// If \p VReg is defined by a G_CONSTANT fits in int64_t returns it.
185LLVM_ABI std::optional<int64_t>
186getIConstantVRegSExtVal(Register VReg, const MachineRegisterInfo &MRI);
187
188/// \p VReg is defined by a G_CONSTANT, return the corresponding value.
189LLVM_ABI const APInt &getIConstantFromReg(Register VReg,
190 const MachineRegisterInfo &MRI);
191
192/// Simple struct used to hold a constant integer value and a virtual
193/// register.
198
199/// If \p VReg is defined by a statically evaluable chain of instructions rooted
200/// on a G_CONSTANT returns its APInt value and def register.
201LLVM_ABI std::optional<ValueAndVReg>
204 bool LookThroughInstrs = true);
205
206/// If \p VReg is defined by a statically evaluable chain of instructions rooted
207/// on a G_CONSTANT or G_FCONSTANT returns its value as APInt and def register.
208LLVM_ABI std::optional<ValueAndVReg> getAnyConstantVRegValWithLookThrough(
209 Register VReg, const MachineRegisterInfo &MRI,
210 bool LookThroughInstrs = true, bool LookThroughAnyExt = false);
211
213 std::tuple<Register, Register, uint64_t, Align, bool, std::vector<LLT>>;
214
215/// Matcher for memcpy-like instructions. For non-zero lengths, \p MemOps
216/// contains the load/store types to emit.
219 unsigned MaxLen, Register &Dst,
220 Register &Src, uint64_t &KnownLen,
221 Align &Alignment, bool &DstAlignCanChange,
222 std::vector<LLT> &MemOps);
223
228
229/// If \p VReg is defined by a statically evaluable chain of instructions rooted
230/// on a G_FCONSTANT returns its APFloat value and def register. The value uses
231/// \p VReg's FP semantics when it has a known FP type, otherwise the
232/// G_FCONSTANT's unless the chain changed the bit width.
233LLVM_ABI std::optional<FPValueAndVReg>
236 bool LookThroughInstrs = true);
237
239 const MachineRegisterInfo &MRI);
240
241/// See if Reg is defined by an single def instruction that is
242/// Opcode. Also try to do trivial folding if it's a COPY with
243/// same types. Returns null otherwise.
245 const MachineRegisterInfo &MRI);
246
247/// Simple struct used to hold a Register value and the instruction which
248/// defines it.
253
254/// Find the def instruction for \p Reg, and underlying value Register folding
255/// away any copies.
256///
257/// Also walks through hints such as G_ASSERT_ZEXT.
258LLVM_ABI std::optional<DefinitionAndSourceRegister>
260
261/// Find the def instruction for \p Reg, folding away any trivial copies. May
262/// return nullptr if \p Reg is not a generic virtual register.
263///
264/// Also walks through hints such as G_ASSERT_ZEXT.
266 const MachineRegisterInfo &MRI);
267
268/// Find the source register for \p Reg, folding away any trivial copies. It
269/// will be an output register of the instruction that getDefIgnoringCopies
270/// returns. May return an invalid register if \p Reg is not a generic virtual
271/// register.
272///
273/// Also walks through hints such as G_ASSERT_ZEXT.
275 const MachineRegisterInfo &MRI);
276
277/// Helper function to split a wide generic register into bitwise blocks with
278/// the given Type (which implies the number of blocks needed). The generic
279/// registers created are appended to Ops, starting at bit 0 of Reg.
280LLVM_ABI void extractParts(Register Reg, LLT Ty, int NumParts,
282 MachineIRBuilder &MIRBuilder,
284
285/// Version which handles irregular splits.
286LLVM_ABI bool extractParts(Register Reg, LLT RegTy, LLT MainTy, LLT &LeftoverTy,
288 SmallVectorImpl<Register> &LeftoverVRegs,
289 MachineIRBuilder &MIRBuilder,
291
292/// Version which handles irregular sub-vector splits.
293LLVM_ABI void extractVectorParts(Register Reg, unsigned NumElts,
295 MachineIRBuilder &MIRBuilder,
297
298// Templated variant of getOpcodeDef returning a MachineInstr derived T.
299/// See if Reg is defined by an single def instruction of type T
300/// Also try to do trivial folding if it's a COPY with
301/// same types. Returns null otherwise.
302template <class T>
307
308/// Modify analysis usage so it preserves passes required for the SelectionDAG
309/// fallback.
310LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU);
311
312LLVM_ABI std::optional<APInt> ConstantFoldBinOp(unsigned Opcode,
313 const Register Op1,
314 const Register Op2,
315 const MachineRegisterInfo &MRI);
316LLVM_ABI std::optional<APFloat>
317ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2,
318 const MachineRegisterInfo &MRI);
319
320/// Tries to constant fold a vector binop with sources \p Op1 and \p Op2.
321/// Returns an empty vector on failure.
323ConstantFoldVectorBinop(unsigned Opcode, const Register Op1, const Register Op2,
324 const MachineRegisterInfo &MRI);
325
326LLVM_ABI std::optional<APInt>
327ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0,
328 const MachineRegisterInfo &MRI);
329
330LLVM_ABI std::optional<APInt> ConstantFoldExtOp(unsigned Opcode,
331 const Register Op1,
333 const MachineRegisterInfo &MRI);
334
335LLVM_ABI std::optional<APFloat>
336ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src,
337 const MachineRegisterInfo &MRI);
338
339/// Tries to constant fold a unary integer operation (G_CTLZ, G_CTTZ, G_CTPOP
340/// and their _ZERO_POISON variants, G_ABS, G_BSWAP, G_BITREVERSE) on \p Src.
341/// If \p Src is a vector then it tries to do an element-wise constant fold.
343ConstantFoldUnaryIntOp(unsigned Opcode, LLT DstTy, Register Src,
344 const MachineRegisterInfo &MRI);
345
346LLVM_ABI std::optional<SmallVector<APInt>>
347ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2,
348 unsigned DstScalarSizeInBits, unsigned ExtOp,
349 const MachineRegisterInfo &MRI);
350
351/// Test if the given value is known to have exactly one bit set. This differs
352/// from computeKnownBits in that it doesn't necessarily determine which bit is
353/// set. When \p OrNegative is true, the value is also considered a power of two
354/// if its negation is a power of two (i.e. its absolute value is a power of
355/// two).
356LLVM_ABI bool
357isKnownToBeAPowerOfTwo(Register Val, const MachineRegisterInfo &MRI,
358 GISelValueTracking *ValueTracking = nullptr,
359 bool OrNegative = false);
360
362 const MachinePointerInfo &MPO);
363
364/// Return a virtual register corresponding to the incoming argument register \p
365/// PhysReg. This register is expected to have class \p RC, and optional type \p
366/// RegTy. This assumes all references to the register will use the same type.
367///
368/// If there is an existing live-in argument register, it will be returned.
369/// This will also ensure there is a valid copy
371 MachineFunction &MF, const TargetInstrInfo &TII, MCRegister PhysReg,
372 const TargetRegisterClass &RC, const DebugLoc &DL, LLT RegTy = LLT());
373
374/// Return the least common multiple type of \p OrigTy and \p TargetTy, by
375/// changing the number of vector elements or scalar bitwidth. The intent is a
376/// G_MERGE_VALUES, G_BUILD_VECTOR, or G_CONCAT_VECTORS can be constructed from
377/// \p OrigTy elements, and unmerged into \p TargetTy. It is an error to call
378/// this function where one argument is a fixed vector and the other is a
379/// scalable vector, since it is illegal to build a G_{MERGE|UNMERGE}_VALUES
380/// between fixed and scalable vectors.
381LLVM_ABI LLVM_READNONE LLT getLCMType(LLT OrigTy, LLT TargetTy);
382
384 /// Return smallest type that covers both \p OrigTy and \p TargetTy and is
385 /// multiple of TargetTy.
386 LLT
387 getCoverTy(LLT OrigTy, LLT TargetTy);
388
389/// Return a type where the total size is the greatest common divisor of \p
390/// OrigTy and \p TargetTy. This will try to either change the number of vector
391/// elements, or bitwidth of scalars. The intent is the result type can be used
392/// as the result of a G_UNMERGE_VALUES from \p OrigTy, and then some
393/// combination of G_MERGE_VALUES, G_BUILD_VECTOR and G_CONCAT_VECTORS (possibly
394/// with intermediate casts) can re-form \p TargetTy.
395///
396/// If these are vectors with different element types, this will try to produce
397/// a vector with a compatible total size, but the element type of \p OrigTy. If
398/// this can't be satisfied, this will produce a scalar smaller than the
399/// original vector elements. It is an error to call this function where
400/// one argument is a fixed vector and the other is a scalable vector, since it
401/// is illegal to build a G_{MERGE|UNMERGE}_VALUES between fixed and scalable
402/// vectors.
403///
404/// In the worst case, this returns LLT::scalar(1)
405LLVM_ABI LLVM_READNONE LLT getGCDType(LLT OrigTy, LLT TargetTy);
406
407/// \returns The splat index of a G_SHUFFLE_VECTOR \p MI when \p MI is a splat.
408/// If \p MI is not a splat, returns std::nullopt.
409LLVM_ABI std::optional<int> getSplatIndex(MachineInstr &MI);
410
411/// \returns the scalar integral splat value of \p Reg if possible.
412LLVM_ABI std::optional<APInt>
413getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI);
414
415/// \returns the scalar integral splat value defined by \p MI if possible.
416LLVM_ABI std::optional<APInt>
417getIConstantSplatVal(const MachineInstr &MI, const MachineRegisterInfo &MRI);
418
419/// \returns the scalar sign extended integral splat value of \p Reg if
420/// possible.
421LLVM_ABI std::optional<int64_t>
422getIConstantSplatSExtVal(const Register Reg, const MachineRegisterInfo &MRI);
423
424/// \returns the scalar sign extended integral splat value defined by \p MI if
425/// possible.
426LLVM_ABI std::optional<int64_t>
427getIConstantSplatSExtVal(const MachineInstr &MI,
428 const MachineRegisterInfo &MRI);
429
430/// Returns a floating point scalar constant of a build vector splat if it
431/// exists. When \p AllowUndef == true some elements can be undef but not all.
432LLVM_ABI std::optional<FPValueAndVReg>
433getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI,
434 bool AllowUndef = true);
435
436/// Return true if the specified register is defined by G_BUILD_VECTOR or
437/// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
439 const MachineRegisterInfo &MRI,
440 int64_t SplatValue, bool AllowUndef);
441
442/// Return true if the specified register is defined by G_BUILD_VECTOR or
443/// G_BUILD_VECTOR_TRUNC where all of the elements are \p SplatValue or undef.
445 const MachineRegisterInfo &MRI,
446 const APInt &SplatValue,
447 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.
451LLVM_ABI bool 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 \p SplatValue or undef.
457LLVM_ABI bool isBuildVectorConstantSplat(const MachineInstr &MI,
458 const MachineRegisterInfo &MRI,
459 const APInt &SplatValue,
460 bool AllowUndef);
461
462/// Return true if the specified instruction is a G_BUILD_VECTOR or
463/// G_BUILD_VECTOR_TRUNC where all of the elements are 0 or undef.
464LLVM_ABI bool isBuildVectorAllZeros(const MachineInstr &MI,
465 const MachineRegisterInfo &MRI,
466 bool AllowUndef = false);
467
468/// Return true if the specified instruction is a G_BUILD_VECTOR or
469/// G_BUILD_VECTOR_TRUNC where all of the elements are ~0 or undef.
470LLVM_ABI bool isBuildVectorAllOnes(const MachineInstr &MI,
471 const MachineRegisterInfo &MRI,
472 bool AllowUndef = false);
473
474/// Return true if the specified instruction is known to be a constant, or a
475/// vector of constants.
476///
477/// If \p AllowFP is true, this will consider G_FCONSTANT in addition to
478/// G_CONSTANT. If \p AllowOpaqueConstants is true, constant-like instructions
479/// such as G_GLOBAL_VALUE will also be considered.
480LLVM_ABI bool isConstantOrConstantVector(const MachineInstr &MI,
481 const MachineRegisterInfo &MRI,
482 bool AllowFP = true,
483 bool AllowOpaqueConstants = true);
484
485/// Return true if the value is a constant 0 integer or a splatted vector of a
486/// constant 0 integer (with no undefs if \p AllowUndefs is false). This will
487/// handle G_BUILD_VECTOR and G_BUILD_VECTOR_TRUNC as truncation is not an issue
488/// for null values.
489LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI,
490 const MachineRegisterInfo &MRI,
491 bool AllowUndefs = false);
492
493/// Return true if the value is a constant -1 integer or a splatted vector of a
494/// constant -1 integer (with no undefs if \p AllowUndefs is false).
495LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI,
496 const MachineRegisterInfo &MRI,
497 bool AllowUndefs = false);
498
499/// \returns a value when \p MI is a vector splat.
500LLVM_ABI std::optional<Register> getVectorSplat(const MachineInstr &MI,
501 const MachineRegisterInfo &MRI);
502
503/// Determines if \p MI defines a constant integer or a build vector of
504/// constant integers. Treats undef values as constants.
505LLVM_ABI bool isConstantOrConstantVector(MachineInstr &MI,
506 const MachineRegisterInfo &MRI);
507
508/// Determines if \p Def defines a constant integer or a splat vector of
509/// constant integers.
510/// \returns the scalar constant or std::nullopt.
511LLVM_ABI std::optional<APInt>
512isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI);
513
514/// Determines if \p Def defines a float constant integer or a splat vector of
515/// float constant integers.
516/// \returns the float constant or std::nullopt.
517LLVM_ABI std::optional<APFloat>
518isConstantOrConstantSplatVectorFP(Register Def, const MachineRegisterInfo &MRI);
519
520/// Attempt to match a unary predicate against a scalar/splat constant or every
521/// element of a constant G_BUILD_VECTOR. If \p ConstVal is null, the source
522/// value was undef.
523LLVM_ABI bool
524matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg,
525 llvm::function_ref<bool(const Constant *ConstVal)> Match,
526 bool AllowUndefs = false);
527
528/// Returns true if given the TargetLowering's boolean contents information,
529/// the value \p Val contains a true value.
530LLVM_ABI bool isConstTrueVal(const TargetLowering &TLI, int64_t Val,
531 bool IsVector, bool IsFP);
532/// \returns true if given the TargetLowering's boolean contents information,
533/// the value \p Val contains a false value.
534LLVM_ABI bool isConstFalseVal(const TargetLowering &TLI, int64_t Val,
535 bool IsVector, bool IsFP);
536
537/// Returns an integer representing true, as defined by the
538/// TargetBooleanContents.
539LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector,
540 bool IsFP);
541
544 LostDebugLocObserver *LocObserver,
545 SmallInstListTy &DeadInstChain);
548 LostDebugLocObserver *LocObserver = nullptr);
550 LostDebugLocObserver *LocObserver = nullptr);
551
552/// Assuming the instruction \p MI is going to be deleted, attempt to salvage
553/// debug users of \p MI by writing the effect of \p MI in a DIExpression.
556
557/// Returns whether opcode \p Opc is a pre-isel generic floating-point opcode,
558/// having only floating-point operands.
560
561/// Returns true if \p Reg can create undef or poison from non-undef &
562/// non-poison operands. \p ConsiderFlagsAndMetadata controls whether poison
563/// producing flags and metadata on the instruction are considered. This can be
564/// used to see if the instruction could still introduce undef or poison even
565/// without poison generating flags and metadata which might be on the
566/// 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.
583 unsigned Depth = 0);
584
585/// Returns true if \p Reg cannot be undef, but may be poison.
588 unsigned Depth = 0);
589
590/// Get the type back from LLT. It won't be 100 percent accurate but returns an
591/// estimate of the type.
593
594/// Returns true if the instruction \p MI is one of the assert
595/// instructions.
597
598/// An integer-like constant.
599///
600/// It abstracts over scalar, fixed-length vectors, and scalable vectors.
601/// In the common case, it provides a common API and feels like an APInt,
602/// while still providing low-level access.
603/// It can be used for constant-folding.
604///
605/// bool isZero()
606/// abstracts over the kind.
607///
608/// switch(const.getKind())
609/// {
610/// }
611/// provides low-level access.
613public:
615
616private:
617 GIConstantKind Kind;
619 APInt Value;
620
621public:
623 : Kind(GIConstantKind::FixedVector), Values(Values) {};
624 GIConstant(const APInt &Value, GIConstantKind Kind)
625 : Kind(Kind), Value(Value) {};
626
627 /// Returns the kind of of this constant, e.g, Scalar.
628 GIConstantKind getKind() const { return Kind; }
629
630 /// Returns the value, if this constant is a scalar.
632
633 LLVM_ABI static std::optional<GIConstant>
635};
636
637/// An floating-point-like constant.
638///
639/// It abstracts over scalar, fixed-length vectors, and scalable vectors.
640/// In the common case, it provides a common API and feels like an APFloat,
641/// while still providing low-level access.
642/// It can be used for constant-folding.
643///
644/// bool isZero()
645/// abstracts over the kind.
646///
647/// switch(const.getKind())
648/// {
649/// }
650/// provides low-level access.
652 using VecTy = SmallVector<APFloat>;
653 using const_iterator = VecTy::const_iterator;
654
655public:
657
658private:
659 GFConstantKind Kind;
661
662public:
664 : Kind(GFConstantKind::FixedVector), Values(Values) {};
665 GFConstant(const APFloat &Value, GFConstantKind Kind) : Kind(Kind) {
666 Values.push_back(Value);
667 }
668
669 /// Returns the kind of of this constant, e.g, Scalar.
670 GFConstantKind getKind() const { return Kind; }
671
672 const_iterator begin() const {
674 "Expected fixed vector or scalar constant");
675 return Values.begin();
676 }
677
678 const_iterator end() const {
680 "Expected fixed vector or scalar constant");
681 return Values.end();
682 }
683
684 size_t size() const {
685 assert(Kind == GFConstantKind::FixedVector && "Expected fixed vector");
686 return Values.size();
687 }
688
689 /// Returns the value, if this constant is a scalar.
691
692 LLVM_ABI static std::optional<GFConstant>
694};
695
696} // End namespace llvm.
697#endif
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_READNONE
Definition Compiler.h:327
#define LLVM_ABI
Definition Compiler.h:211
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:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
uint64_t IntrinsicInst * II
MachineRegisterInfo & MRI
const char * Msg
static const char PassName[]
Class for arbitrary precision integers.
Definition APInt.h:78
Represent the analysis usage information of a pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const_iterator begin() const
Definition Utils.h:672
static LLVM_ABI std::optional< GFConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2063
GFConstant(const APFloat &Value, GFConstantKind Kind)
Definition Utils.h:665
GFConstant(ArrayRef< APFloat > Values)
Definition Utils.h:663
GFConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:670
LLVM_ABI APFloat getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2056
const_iterator end() const
Definition Utils.h:678
size_t size() const
Definition Utils.h:684
LLVM_ABI APInt getScalarValue() const
Returns the value, if this constant is a scalar.
Definition Utils.cpp:2016
GIConstant(const APInt &Value, GIConstantKind Kind)
Definition Utils.h:624
static LLVM_ABI std::optional< GIConstant > getConstant(Register Const, const MachineRegisterInfo &MRI)
Definition Utils.cpp:2023
GIConstantKind getKind() const
Returns the kind of of this constant, e.g, Scalar.
Definition Utils.h:628
GIConstant(ArrayRef< APInt > Values)
Definition Utils.h:622
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
Describe properties that are true of each instruction in the target description file.
MCRegisterClass - Base class of TargetRegisterClass.
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
Diagnostic information for missed-optimization remarks.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Analysis providing profile information.
Holds all the information related to register banks.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Target-Independent Code Generator Pass Configuration Options.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
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.
Definition Utils.cpp:867
LLVM_ABI std::optional< SmallVector< APInt > > ConstantFoldICmp(unsigned Pred, const Register Op1, const Register Op2, unsigned DstScalarSizeInBits, unsigned ExtOp, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1002
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a constant integer or a splat vector of constant integers.
Definition Utils.cpp:1537
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...
Definition Utils.cpp:1453
LLVM_ABI Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
Definition Utils.cpp:1998
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...
Definition Utils.cpp:60
LLVM_ABI 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:675
LLVM_ABI std::optional< APFloat > isConstantOrConstantSplatVectorFP(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a float constant integer or a splat vector of float constant integers.
Definition Utils.cpp:1549
LLVM_ABI const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:483
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.
Definition Utils.cpp:297
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
LLVM_ABI std::optional< APFloat > ConstantFoldIntToFloat(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Definition Utils.cpp:941
LLVM_ABI std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1413
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...
Definition Utils.cpp:1577
LLVM_ABI std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition Utils.cpp:750
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...
Definition Utils.cpp:1697
LLVM_ABI void 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:159
LLVM_ABI bool matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg, llvm::function_ref< 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:1592
LLVM_ABI std::optional< APInt > ConstantFoldExtOp(unsigned Opcode, const Register Op1, uint64_t Imm, const MachineRegisterInfo &MRI)
Definition Utils.cpp:900
GISelWorkList< 4 > SmallInstListTy
Definition Utils.h:542
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...
Definition Utils.cpp:1559
LLVM_ABI MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
Definition Utils.cpp:516
LLVM_ABI void reportGISelWarning(MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel warning as a missed optimization remark to the LLVMContext's diagnostic stream.
Definition Utils.cpp:255
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...
Definition Utils.cpp:1625
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...
Definition Utils.cpp:1160
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CtxI=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 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:317
LLVM_ABI std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
Definition Utils.cpp:681
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
LLVM_ABI const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
Definition Utils.cpp:308
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.
Definition Utils.cpp:1517
LLVM_ABI bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
Definition Utils.cpp:203
LLVM_ABI void saveUsesAndErase(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver, SmallInstListTy &DeadInstChain)
Definition Utils.cpp:1662
LLVM_ABI void reportGISelFailure(MachineFunction &MF, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream.
Definition Utils.cpp:261
std::tuple< Register, Register, uint64_t, Align, bool, std::vector< LLT > > MemCpyFamilyLoweringInfo
Definition Utils.h:212
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...
Definition Utils.cpp:442
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...
Definition Utils.cpp:1459
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...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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.
Definition Utils.cpp:828
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.
Definition Utils.cpp:1446
LLVM_ABI std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
Definition Utils.cpp:917
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...
Definition Utils.cpp:530
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1156
LLVM_ABI bool canLowerMemCpyFamily(const MachineInstr &MI, const MachineRegisterInfo &MRI, unsigned MaxLen, Register &Dst, Register &Src, uint64_t &KnownLen, Align &Alignment, bool &DstAlignCanChange, std::vector< LLT > &MemOps)
Matcher for memcpy-like instructions.
Definition Utils.cpp:2165
LLVM_ABI 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:1227
LLVM_ABI unsigned getInverseGMinMaxOpcode(unsigned MinMaxOpc)
Returns the inverse opcode of MinMaxOpc, which is a generic min/max opcode like G_SMIN.
Definition Utils.cpp:282
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...
Definition Utils.cpp:450
LLVM_ABI bool isConstFalseVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Definition Utils.cpp:1638
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 ...
Definition Utils.cpp:1372
LLVM_ABI void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition Utils.cpp:1692
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.
Definition Utils.cpp:50
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
Definition Utils.cpp:1650
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...
Definition Utils.cpp:436
LLVM_ABI bool isPreISelGenericFloatingPointOpcode(unsigned Opc)
Returns whether opcode Opc is a pre-isel generic floating-point opcode, having only floating-point op...
Definition Utils.cpp:1720
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.
Definition Utils.cpp:491
LLVM_ABI SmallVector< APInt > ConstantFoldUnaryIntOp(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Tries to constant fold a unary integer operation (G_CTLZ, G_CTTZ, G_CTPOP and their _ZERO_POISON vari...
Definition Utils.cpp:954
LLVM_ABI std::optional< Register > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1465
LLVM_ABI void eraseInstrs(ArrayRef< MachineInstr * > DeadInstrs, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
Definition Utils.cpp:1677
LLVM_ABI Register getSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the source register for Reg, folding away any trivial copies.
Definition Utils.cpp:523
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.
Definition Utils.cpp:1248
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)
Definition Utils.cpp:1431
LLVM_ABI bool isAssertMI(const MachineInstr &MI)
Returns true if the instruction MI is one of the assert instructions.
Definition Utils.cpp:2005
LLVM_ABI void extractVectorParts(Register Reg, unsigned NumElts, SmallVectorImpl< Register > &VRegs, MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI)
Version which handles irregular sub-vector splits.
Definition Utils.cpp:633
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...
Definition Utils.cpp:224
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI Align inferAlignFromPtrInfo(MachineFunction &MF, const MachinePointerInfo &MPO)
Definition Utils.cpp:850
#define MORE()
Definition regcomp.c:247
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Simple struct used to hold a Register value and the instruction which defines it.
Definition Utils.h:249
This class contains a discriminated union of information about pointers in memory operands,...
Simple struct used to hold a constant integer value and a virtual register.
Definition Utils.h:194
Register VReg
Definition Utils.h:196