27#define DEBUG_TYPE "x86-fixup-vector-constants"
29STATISTIC(NumInstChanges,
"Number of instructions changes");
38 StringRef getPassName()
const override {
39 return "X86 Fixup Vector Constants";
42 bool runOnMachineFunction(MachineFunction &MF)
override;
43 bool processInstruction(MachineFunction &MF, MachineBasicBlock &
MBB,
47 MachineFunctionProperties getRequiredProperties()
const override {
48 return MachineFunctionProperties().setNoVRegs();
52 const X86InstrInfo *TII =
nullptr;
53 const X86Subtarget *ST =
nullptr;
54 const MCSchedModel *SM =
nullptr;
58char X86FixupVectorConstantsPass::ID = 0;
63 return new X86FixupVectorConstantsPass();
85 unsigned NumBits =
C->getType()->getPrimitiveSizeInBits();
94 return CInt->getValue();
101 return CFP->getValue().bitcastToAPInt();
107 assert((NumBits % Bits->getBitWidth()) == 0 &&
"Illegal splat");
113 for (
unsigned I = 0,
E = CV->getNumOperands();
I !=
E; ++
I) {
118 assert(NumBits == (
E * SubBits->getBitWidth()) &&
119 "Illegal vector element size");
120 Bits.insertBits(*SubBits,
I * SubBits->getBitWidth());
126 bool IsInteger = CDS->getElementType()->isIntegerTy();
127 bool IsFloat = CDS->getElementType()->isHalfTy() ||
128 CDS->getElementType()->isBFloatTy() ||
129 CDS->getElementType()->isFloatTy() ||
130 CDS->getElementType()->isDoubleTy();
131 if (IsInteger || IsFloat) {
133 unsigned EltBits = CDS->getElementType()->getPrimitiveSizeInBits();
134 for (
unsigned I = 0,
E = CDS->getNumElements();
I !=
E; ++
I) {
136 Bits.insertBits(CDS->getElementAsAPInt(
I),
I * EltBits);
138 Bits.insertBits(CDS->getElementAsAPFloat(
I).bitcastToAPInt(),
151 return Bits->zextOrTrunc(NumBits);
158 unsigned SplatBitWidth) {
159 const Type *Ty =
C->getType();
160 assert((Ty->getPrimitiveSizeInBits() % SplatBitWidth) == 0 &&
161 "Illegal splat width");
164 if (Bits->isSplat(SplatBitWidth))
165 return Bits->trunc(SplatBitWidth);
170 unsigned NumOps = CV->getNumOperands();
171 unsigned NumEltsBits = Ty->getScalarSizeInBits();
172 unsigned NumScaleOps = SplatBitWidth / NumEltsBits;
173 if ((SplatBitWidth % NumEltsBits) == 0) {
177 for (
unsigned Idx = 0; Idx !=
NumOps; ++Idx) {
181 unsigned SplatIdx = Idx % NumScaleOps;
182 if (!Sequence[SplatIdx] || Sequence[SplatIdx] == Elt) {
183 Sequence[SplatIdx] = Elt;
192 for (
unsigned I = 0;
I != NumScaleOps; ++
I) {
196 SplatBits.
insertBits(*Bits,
I * Bits->getBitWidth());
211 const APInt &Bits,
unsigned NumSclBits) {
212 unsigned BitWidth = Bits.getBitWidth();
214 if (NumSclBits == 8) {
217 RawBits.
push_back(Bits.extractBits(8,
I).getZExtValue());
221 if (NumSclBits == 16) {
224 RawBits.
push_back(Bits.extractBits(16,
I).getZExtValue());
230 if (NumSclBits == 32) {
233 RawBits.
push_back(Bits.extractBits(32,
I).getZExtValue());
239 assert(NumSclBits == 64 &&
"Unhandled vector element width");
243 RawBits.
push_back(Bits.extractBits(64,
I).getZExtValue());
252 unsigned ,
unsigned SplatBitWidth) {
260 Type *SclTy =
C->getType()->getScalarType();
262 NumSclBits = std::min<unsigned>(NumSclBits, SplatBitWidth);
265 NumSclBits = (NumSclBits == 8 || NumSclBits == 16 || NumSclBits == 32)
275 unsigned ScalarBitWidth) {
276 Type *SclTy =
C->getType()->getScalarType();
280 if (NumBits > ScalarBitWidth) {
283 if (Bits->countLeadingZeros() >= (NumBits - ScalarBitWidth)) {
286 if (ScalarBitWidth > NumSclBits && (ScalarBitWidth % NumSclBits) == 0)
290 APInt RawBits = Bits->zextOrTrunc(ScalarBitWidth);
291 return ConstantInt::get(Ctx, RawBits);
300 unsigned NumBits,
unsigned NumElts,
301 unsigned SrcEltBitWidth) {
302 unsigned DstEltBitWidth = NumBits / NumElts;
303 assert((NumBits % NumElts) == 0 && (NumBits % SrcEltBitWidth) == 0 &&
304 (DstEltBitWidth % SrcEltBitWidth) == 0 &&
305 (DstEltBitWidth > SrcEltBitWidth) &&
"Illegal extension width");
308 assert((Bits->getBitWidth() / DstEltBitWidth) == NumElts &&
309 (Bits->getBitWidth() % DstEltBitWidth) == 0 &&
310 "Unexpected constant extension");
314 for (
unsigned I = 0;
I != NumElts; ++
I) {
315 APInt Elt = Bits->extractBits(DstEltBitWidth,
I * DstEltBitWidth);
322 Type *Ty =
C->getType();
323 return rebuildConstant(Ty->getContext(), Ty->getScalarType(), TruncBits,
330 unsigned NumElts,
unsigned SrcEltBitWidth) {
334 unsigned NumElts,
unsigned SrcEltBitWidth) {
338bool X86FixupVectorConstantsPass::processInstruction(
MachineFunction &MF,
341 unsigned Opc =
MI.getOpcode();
342 MachineConstantPool *
CP =
MI.getParent()->getParent()->getConstantPool();
346 bool HasDQI = ST->hasDQI();
347 bool HasBWI = ST->hasBWI();
348 bool HasVLX = ST->hasVLX();
360 auto NewOpcPreferable = [&](
const FixupEntry &
Fixup,
361 unsigned RegBitWidth) ->
bool {
363 unsigned NewOpc =
Fixup.Op;
366 unsigned BitsSaved = RegBitWidth - (
Fixup.NumCstElts *
Fixup.MemBitWidth);
374 if (OldTput != NewTput)
375 return NewTput < OldTput;
377 int LatTol = (BitsSaved + 127) / 128;
380 if (OldLat != NewLat)
381 return NewLat < (OldLat + LatTol);
390 unsigned OperandNo) {
391#ifdef EXPENSIVE_CHECKS
393 [](
const FixupEntry &
A,
const FixupEntry &
B) {
394 return (
A.NumCstElts *
A.MemBitWidth) <
395 (
B.NumCstElts *
B.MemBitWidth);
397 "Constant fixup table not sorted in ascending constant size");
400 "Unexpected number of operands!");
402 unsigned CstBitWidth =
C->getType()->getPrimitiveSizeInBits();
403 RegBitWidth = RegBitWidth ? RegBitWidth : CstBitWidth;
404 for (
const FixupEntry &
Fixup : Fixups) {
409 if (
Fixup.Op && (OptSize || NewOpcPreferable(
Fixup, RegBitWidth))) {
412 if (Constant *NewCst =
Fixup.RebuildConstant(
413 C, RegBitWidth,
Fixup.NumCstElts,
Fixup.MemBitWidth)) {
415 CP->getConstantPoolIndex(NewCst,
Align(
Fixup.MemBitWidth / 8));
440 case X86::MOVUPSrm: {
445 return FixupConstant(Fixups, 128, 1);
450 case X86::VMOVUPSrm: {
466 return FixupConstant(Fixups, 128, 1);
468 case X86::VMOVAPDYrm:
469 case X86::VMOVAPSYrm:
470 case X86::VMOVUPDYrm:
471 case X86::VMOVUPSYrm: {
474 {HasAVX2 && MultiDomain ? X86::VPMOVSXBQYrm : 0, 4, 8,
rebuildSExtCst},
475 {HasAVX2 && MultiDomain ? X86::VPMOVZXBQYrm : 0, 4, 8,
rebuildZExtCst},
477 {HasAVX2 && MultiDomain ? X86::VPMOVSXBDYrm : 0, 8, 8,
rebuildSExtCst},
478 {HasAVX2 && MultiDomain ? X86::VPMOVZXBDYrm : 0, 8, 8,
rebuildZExtCst},
479 {HasAVX2 && MultiDomain ? X86::VPMOVSXWQYrm : 0, 4, 16,
rebuildSExtCst},
480 {HasAVX2 && MultiDomain ? X86::VPMOVZXWQYrm : 0, 4, 16,
rebuildZExtCst},
482 {HasAVX2 && MultiDomain ? X86::VPMOVSXWDYrm : 0, 8, 16,
rebuildSExtCst},
483 {HasAVX2 && MultiDomain ? X86::VPMOVZXWDYrm : 0, 8, 16,
rebuildZExtCst},
484 {HasAVX2 && MultiDomain ? X86::VPMOVSXDQYrm : 0, 4, 32,
rebuildSExtCst},
485 {HasAVX2 && MultiDomain ? X86::VPMOVZXDQYrm : 0, 4, 32,
487 return FixupConstant(Fixups, 256, 1);
489 case X86::VMOVAPDZ128rm:
490 case X86::VMOVAPSZ128rm:
491 case X86::VMOVUPDZ128rm:
492 case X86::VMOVUPSZ128rm: {
508 return FixupConstant(Fixups, 128, 1);
510 case X86::VMOVAPDZ256rm:
511 case X86::VMOVAPSZ256rm:
512 case X86::VMOVUPDZ256rm:
513 case X86::VMOVUPSZ256rm: {
528 return FixupConstant(Fixups, 256, 1);
530 case X86::VMOVAPDZrm:
531 case X86::VMOVAPSZrm:
532 case X86::VMOVUPDZrm:
533 case X86::VMOVUPSZrm: {
549 return FixupConstant(Fixups, 512, 1);
553 case X86::MOVDQUrm: {
569 return FixupConstant(Fixups, 128, 1);
572 case X86::VMOVDQUrm: {
579 {HasAVX2 ? X86::VPBROADCASTDrm : X86::VBROADCASTSSrm, 1, 32,
586 {HasAVX2 ? X86::VPBROADCASTQrm : X86::VMOVDDUPrm, 1, 64,
594 return FixupConstant(Fixups, 128, 1);
596 case X86::VMOVDQAYrm:
597 case X86::VMOVDQUYrm: {
601 {HasAVX2 ? X86::VPBROADCASTDYrm : X86::VBROADCASTSSYrm, 1, 32,
605 {HasAVX2 ? X86::VPBROADCASTQYrm : X86::VBROADCASTSDYrm, 1, 64,
611 {HasAVX2 ? X86::VBROADCASTI128rm : X86::VBROADCASTF128rm, 1, 128,
619 return FixupConstant(Fixups, 256, 1);
621 case X86::VMOVDQA32Z128rm:
622 case X86::VMOVDQA64Z128rm:
623 case X86::VMOVDQU32Z128rm:
624 case X86::VMOVDQU64Z128rm: {
644 return FixupConstant(Fixups, 128, 1);
646 case X86::VMOVDQA32Z256rm:
647 case X86::VMOVDQA64Z256rm:
648 case X86::VMOVDQU32Z256rm:
649 case X86::VMOVDQU64Z256rm: {
668 return FixupConstant(Fixups, 256, 1);
670 case X86::VMOVDQA32Zrm:
671 case X86::VMOVDQA64Zrm:
672 case X86::VMOVDQU32Zrm:
673 case X86::VMOVDQU64Zrm: {
693 return FixupConstant(Fixups, 512, 1);
697 auto ConvertToBroadcast = [&](
unsigned OpSrc,
int BW) {
699 if (
const X86FoldTableEntry *Mem2Bcst =
701 unsigned OpBcst = Mem2Bcst->DstOp;
706 return FixupConstant(Fixups, 0, OpNoBcst);
715 return ConvertToBroadcast(
Opc, 32) || ConvertToBroadcast(
Opc, 64);
719 if (HasVLX && !HasDQI) {
720 unsigned OpSrc32 = 0, OpSrc64 = 0;
725 OpSrc32 = X86 ::VPANDDZ128rm;
726 OpSrc64 = X86 ::VPANDQZ128rm;
731 OpSrc32 = X86 ::VPANDDZ256rm;
732 OpSrc64 = X86 ::VPANDQZ256rm;
737 OpSrc32 = X86 ::VPANDNDZ128rm;
738 OpSrc64 = X86 ::VPANDNQZ128rm;
740 case X86::VANDNPDYrm:
741 case X86::VANDNPSYrm:
743 OpSrc32 = X86 ::VPANDNDZ256rm;
744 OpSrc64 = X86 ::VPANDNQZ256rm;
749 OpSrc32 = X86 ::VPORDZ128rm;
750 OpSrc64 = X86 ::VPORQZ128rm;
755 OpSrc32 = X86 ::VPORDZ256rm;
756 OpSrc64 = X86 ::VPORQZ256rm;
761 OpSrc32 = X86 ::VPXORDZ128rm;
762 OpSrc64 = X86 ::VPXORQZ128rm;
767 OpSrc32 = X86 ::VPXORDZ256rm;
768 OpSrc64 = X86 ::VPXORQZ256rm;
771 if (OpSrc32 || OpSrc64)
772 return ConvertToBroadcast(OpSrc32, 32) || ConvertToBroadcast(OpSrc64, 64);
778bool X86FixupVectorConstantsPass::runOnMachineFunction(MachineFunction &MF) {
783 SM = &ST->getSchedModel();
785 for (MachineBasicBlock &
MBB : MF) {
786 for (MachineInstr &
MI :
MBB) {
787 if (processInstruction(MF,
MBB,
MI)) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
PowerPC TLS Dynamic Call Fixup
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static Constant * rebuildSplatCst(const Constant *C, unsigned, unsigned, unsigned SplatBitWidth)
static std::optional< APInt > getSplatableConstant(const Constant *C, unsigned SplatBitWidth)
static Constant * rebuildZExtCst(const Constant *C, unsigned NumBits, unsigned NumElts, unsigned SrcEltBitWidth)
static std::optional< APInt > extractConstantBits(const Constant *C)
static Constant * getSplatValueAllowUndef(const ConstantVector *C)
Normally, we only allow poison in vector splats.
static Constant * rebuildExtCst(const Constant *C, bool IsSExt, unsigned NumBits, unsigned NumElts, unsigned SrcEltBitWidth)
static Constant * rebuildZeroUpperCst(const Constant *C, unsigned NumBits, unsigned, unsigned ScalarBitWidth)
static Constant * rebuildSExtCst(const Constant *C, unsigned NumBits, unsigned NumElts, unsigned SrcEltBitWidth)
static Constant * rebuildConstant(LLVMContext &Ctx, Type *SclTy, const APInt &Bits, unsigned NumSclBits)
Class for arbitrary precision integers.
unsigned getActiveBits() const
Compute the number of active bits in the value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
static LLVM_ABI Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
static LLVM_ABI Constant * getFP(Type *ElementType, ArrayRef< uint16_t > Elts)
getFP() constructors - Return a constant of vector type with a float element type taken from argument...
Constant Vector Declarations.
This is an important base class in LLVM.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
This is an important class for using LLVM in a threaded context.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
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.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
bool is16bitFPTy() const
Return true if this is a 16-bit float type.
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Value * getOperand(unsigned i) const
LLVM Value Representation.
const X86InstrInfo * getInstrInfo() const override
bool hasNoDomainDelayMov() const
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
@ EVEX
EVEX - Specifies that this instruction use EVEX form which provides syntax support up to 32 512-bit r...
const Constant * getConstantFromPool(const MachineInstr &MI, unsigned OpNo)
Find any constant pool entry associated with a specific instruction operand.
This is an optimization pass for GlobalISel generic memory operations.
const X86FoldTableEntry * lookupBroadcastFoldTableBySize(unsigned MemOp, unsigned BroadcastBits)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
FunctionPass * createX86FixupVectorConstants()
Return a pass that reduces the size of vector constant pool loads.
const MCSchedClassDesc * getSchedClassDesc(unsigned SchedClassIdx) const
bool hasInstrSchedModel() const
Does this machine model include instruction-level scheduling.
static LLVM_ABI int computeInstrLatency(const MCSubtargetInfo &STI, const MCSchedClassDesc &SCDesc)
Returns the latency value for the scheduling class.
static LLVM_ABI double getReciprocalThroughput(const MCSubtargetInfo &STI, const MCSchedClassDesc &SCDesc)