27#define DEBUG_TYPE "x86-fixup-vector-constants"
29STATISTIC(NumInstChanges,
"Number of instructions changes");
32class X86FixupVectorConstantsImpl {
49 X86FixupVectorConstantsLegacy() : MachineFunctionPass(ID) {}
51 StringRef getPassName()
const override {
52 return "X86 Fixup Vector Constants";
55 bool runOnMachineFunction(MachineFunction &MF)
override;
58 MachineFunctionProperties getRequiredProperties()
const override {
59 return MachineFunctionProperties().setNoVRegs();
64char X86FixupVectorConstantsLegacy::ID = 0;
70 return new X86FixupVectorConstantsLegacy();
92 unsigned NumBits =
C->getType()->getPrimitiveSizeInBits();
101 return CInt->getValue();
108 return CFP->getValue().bitcastToAPInt();
114 assert((NumBits % Bits->getBitWidth()) == 0 &&
"Illegal splat");
120 for (
unsigned I = 0,
E = CV->getNumOperands();
I !=
E; ++
I) {
125 assert(NumBits == (
E * SubBits->getBitWidth()) &&
126 "Illegal vector element size");
127 Bits.insertBits(*SubBits,
I * SubBits->getBitWidth());
133 bool IsInteger = CDS->getElementType()->isIntegerTy();
134 bool IsFloat = CDS->getElementType()->isHalfTy() ||
135 CDS->getElementType()->isBFloatTy() ||
136 CDS->getElementType()->isFloatTy() ||
137 CDS->getElementType()->isDoubleTy();
138 if (IsInteger || IsFloat) {
140 unsigned EltBits = CDS->getElementType()->getPrimitiveSizeInBits();
141 for (
unsigned I = 0,
E = CDS->getNumElements();
I !=
E; ++
I) {
143 Bits.insertBits(CDS->getElementAsAPInt(
I),
I * EltBits);
145 Bits.insertBits(CDS->getElementAsAPFloat(
I).bitcastToAPInt(),
158 return Bits->zextOrTrunc(NumBits);
165 unsigned SplatBitWidth) {
166 const Type *Ty =
C->getType();
167 assert((Ty->getPrimitiveSizeInBits() % SplatBitWidth) == 0 &&
168 "Illegal splat width");
171 if (Bits->isSplat(SplatBitWidth))
172 return Bits->trunc(SplatBitWidth);
177 unsigned NumOps = CV->getNumOperands();
178 unsigned NumEltsBits = Ty->getScalarSizeInBits();
179 unsigned NumScaleOps = SplatBitWidth / NumEltsBits;
180 if ((SplatBitWidth % NumEltsBits) == 0) {
184 for (
unsigned Idx = 0; Idx !=
NumOps; ++Idx) {
188 unsigned SplatIdx = Idx % NumScaleOps;
189 if (!Sequence[SplatIdx] || Sequence[SplatIdx] == Elt) {
190 Sequence[SplatIdx] = Elt;
199 for (
unsigned I = 0;
I != NumScaleOps; ++
I) {
203 SplatBits.
insertBits(*Bits,
I * Bits->getBitWidth());
218 const APInt &Bits,
unsigned NumSclBits) {
219 unsigned BitWidth = Bits.getBitWidth();
221 if (NumSclBits == 8) {
224 RawBits.
push_back(Bits.extractBits(8,
I).getZExtValue());
228 if (NumSclBits == 16) {
231 RawBits.
push_back(Bits.extractBits(16,
I).getZExtValue());
237 if (NumSclBits == 32) {
240 RawBits.
push_back(Bits.extractBits(32,
I).getZExtValue());
246 assert(NumSclBits == 64 &&
"Unhandled vector element width");
250 RawBits.
push_back(Bits.extractBits(64,
I).getZExtValue());
259 unsigned ,
unsigned SplatBitWidth) {
267 Type *SclTy =
C->getType()->getScalarType();
269 NumSclBits = std::min<unsigned>(NumSclBits, SplatBitWidth);
272 NumSclBits = (NumSclBits == 8 || NumSclBits == 16 || NumSclBits == 32)
282 unsigned ScalarBitWidth) {
283 Type *SclTy =
C->getType()->getScalarType();
287 if (NumBits > ScalarBitWidth) {
290 if (Bits->countLeadingZeros() >= (NumBits - ScalarBitWidth)) {
293 if (ScalarBitWidth > NumSclBits && (ScalarBitWidth % NumSclBits) == 0)
297 APInt RawBits = Bits->zextOrTrunc(ScalarBitWidth);
298 return ConstantInt::get(Ctx, RawBits);
307 unsigned NumBits,
unsigned NumElts,
308 unsigned SrcEltBitWidth) {
309 unsigned DstEltBitWidth = NumBits / NumElts;
310 assert((NumBits % NumElts) == 0 && (NumBits % SrcEltBitWidth) == 0 &&
311 (DstEltBitWidth % SrcEltBitWidth) == 0 &&
312 (DstEltBitWidth > SrcEltBitWidth) &&
"Illegal extension width");
315 assert((Bits->getBitWidth() / DstEltBitWidth) == NumElts &&
316 (Bits->getBitWidth() % DstEltBitWidth) == 0 &&
317 "Unexpected constant extension");
321 for (
unsigned I = 0;
I != NumElts; ++
I) {
322 APInt Elt = Bits->extractBits(DstEltBitWidth,
I * DstEltBitWidth);
329 Type *Ty =
C->getType();
330 return rebuildConstant(Ty->getContext(), Ty->getScalarType(), TruncBits,
337 unsigned NumElts,
unsigned SrcEltBitWidth) {
341 unsigned NumElts,
unsigned SrcEltBitWidth) {
345bool X86FixupVectorConstantsImpl::processInstruction(
MachineFunction &MF,
348 unsigned Opc =
MI.getOpcode();
349 MachineConstantPool *CP =
MI.getParent()->getParent()->getConstantPool();
353 bool HasDQI = ST->hasDQI();
354 bool HasBWI = ST->hasBWI();
355 bool HasVLX = ST->hasVLX();
367 auto NewOpcPreferable = [&](
const FixupEntry &
Fixup,
368 unsigned RegBitWidth) ->
bool {
370 unsigned NewOpc =
Fixup.Op;
373 unsigned BitsSaved = RegBitWidth - (
Fixup.NumCstElts *
Fixup.MemBitWidth);
381 if (OldTput != NewTput)
382 return NewTput < OldTput;
384 int LatTol = (BitsSaved + 127) / 128;
387 if (OldLat != NewLat)
388 return NewLat < (OldLat + LatTol);
397 unsigned OperandNo) {
398#ifdef EXPENSIVE_CHECKS
400 [](
const FixupEntry &
A,
const FixupEntry &
B) {
401 return (
A.NumCstElts *
A.MemBitWidth) <
402 (
B.NumCstElts *
B.MemBitWidth);
404 "Constant fixup table not sorted in ascending constant size");
407 "Unexpected number of operands!");
409 unsigned CstBitWidth =
C->getType()->getPrimitiveSizeInBits();
410 RegBitWidth = RegBitWidth ? RegBitWidth : CstBitWidth;
411 for (
const FixupEntry &
Fixup : Fixups) {
416 if (
Fixup.Op && (OptSize || NewOpcPreferable(
Fixup, RegBitWidth))) {
419 if (Constant *NewCst =
Fixup.RebuildConstant(
420 C, RegBitWidth,
Fixup.NumCstElts,
Fixup.MemBitWidth)) {
447 case X86::MOVUPSrm: {
452 return FixupConstant(Fixups, 128, 1);
457 case X86::VMOVUPSrm: {
473 return FixupConstant(Fixups, 128, 1);
475 case X86::VMOVAPDYrm:
476 case X86::VMOVAPSYrm:
477 case X86::VMOVUPDYrm:
478 case X86::VMOVUPSYrm: {
481 {HasAVX2 && MultiDomain ? X86::VPMOVSXBQYrm : 0, 4, 8,
rebuildSExtCst},
482 {HasAVX2 && MultiDomain ? X86::VPMOVZXBQYrm : 0, 4, 8,
rebuildZExtCst},
484 {HasAVX2 && MultiDomain ? X86::VPMOVSXBDYrm : 0, 8, 8,
rebuildSExtCst},
485 {HasAVX2 && MultiDomain ? X86::VPMOVZXBDYrm : 0, 8, 8,
rebuildZExtCst},
486 {HasAVX2 && MultiDomain ? X86::VPMOVSXWQYrm : 0, 4, 16,
rebuildSExtCst},
487 {HasAVX2 && MultiDomain ? X86::VPMOVZXWQYrm : 0, 4, 16,
rebuildZExtCst},
489 {HasAVX2 && MultiDomain ? X86::VPMOVSXWDYrm : 0, 8, 16,
rebuildSExtCst},
490 {HasAVX2 && MultiDomain ? X86::VPMOVZXWDYrm : 0, 8, 16,
rebuildZExtCst},
491 {HasAVX2 && MultiDomain ? X86::VPMOVSXDQYrm : 0, 4, 32,
rebuildSExtCst},
492 {HasAVX2 && MultiDomain ? X86::VPMOVZXDQYrm : 0, 4, 32,
494 return FixupConstant(Fixups, 256, 1);
496 case X86::VMOVAPDZ128rm:
497 case X86::VMOVAPSZ128rm:
498 case X86::VMOVUPDZ128rm:
499 case X86::VMOVUPSZ128rm: {
515 return FixupConstant(Fixups, 128, 1);
517 case X86::VMOVAPDZ256rm:
518 case X86::VMOVAPSZ256rm:
519 case X86::VMOVUPDZ256rm:
520 case X86::VMOVUPSZ256rm: {
535 return FixupConstant(Fixups, 256, 1);
537 case X86::VMOVAPDZrm:
538 case X86::VMOVAPSZrm:
539 case X86::VMOVUPDZrm:
540 case X86::VMOVUPSZrm: {
556 return FixupConstant(Fixups, 512, 1);
560 case X86::MOVDQUrm: {
576 return FixupConstant(Fixups, 128, 1);
579 case X86::VMOVDQUrm: {
586 {HasAVX2 ? X86::VPBROADCASTDrm : X86::VBROADCASTSSrm, 1, 32,
593 {HasAVX2 ? X86::VPBROADCASTQrm : X86::VMOVDDUPrm, 1, 64,
601 return FixupConstant(Fixups, 128, 1);
603 case X86::VMOVDQAYrm:
604 case X86::VMOVDQUYrm: {
608 {HasAVX2 ? X86::VPBROADCASTDYrm : X86::VBROADCASTSSYrm, 1, 32,
612 {HasAVX2 ? X86::VPBROADCASTQYrm : X86::VBROADCASTSDYrm, 1, 64,
618 {HasAVX2 ? X86::VBROADCASTI128rm : X86::VBROADCASTF128rm, 1, 128,
626 return FixupConstant(Fixups, 256, 1);
628 case X86::VMOVDQA32Z128rm:
629 case X86::VMOVDQA64Z128rm:
630 case X86::VMOVDQU32Z128rm:
631 case X86::VMOVDQU64Z128rm: {
651 return FixupConstant(Fixups, 128, 1);
653 case X86::VMOVDQA32Z256rm:
654 case X86::VMOVDQA64Z256rm:
655 case X86::VMOVDQU32Z256rm:
656 case X86::VMOVDQU64Z256rm: {
675 return FixupConstant(Fixups, 256, 1);
677 case X86::VMOVDQA32Zrm:
678 case X86::VMOVDQA64Zrm:
679 case X86::VMOVDQU32Zrm:
680 case X86::VMOVDQU64Zrm: {
700 return FixupConstant(Fixups, 512, 1);
704 auto ConvertToBroadcast = [&](
unsigned OpSrc,
int BW) {
706 if (
const X86FoldTableEntry *Mem2Bcst =
708 unsigned OpBcst = Mem2Bcst->DstOp;
713 return FixupConstant(Fixups, 0, OpNoBcst);
722 return ConvertToBroadcast(
Opc, 32) || ConvertToBroadcast(
Opc, 64);
726 if (HasVLX && !HasDQI) {
727 unsigned OpSrc32 = 0, OpSrc64 = 0;
732 OpSrc32 = X86 ::VPANDDZ128rm;
733 OpSrc64 = X86 ::VPANDQZ128rm;
738 OpSrc32 = X86 ::VPANDDZ256rm;
739 OpSrc64 = X86 ::VPANDQZ256rm;
744 OpSrc32 = X86 ::VPANDNDZ128rm;
745 OpSrc64 = X86 ::VPANDNQZ128rm;
747 case X86::VANDNPDYrm:
748 case X86::VANDNPSYrm:
750 OpSrc32 = X86 ::VPANDNDZ256rm;
751 OpSrc64 = X86 ::VPANDNQZ256rm;
756 OpSrc32 = X86 ::VPORDZ128rm;
757 OpSrc64 = X86 ::VPORQZ128rm;
762 OpSrc32 = X86 ::VPORDZ256rm;
763 OpSrc64 = X86 ::VPORQZ256rm;
768 OpSrc32 = X86 ::VPXORDZ128rm;
769 OpSrc64 = X86 ::VPXORQZ128rm;
774 OpSrc32 = X86 ::VPXORDZ256rm;
775 OpSrc64 = X86 ::VPXORQZ256rm;
778 if (OpSrc32 || OpSrc64)
779 return ConvertToBroadcast(OpSrc32, 32) || ConvertToBroadcast(OpSrc64, 64);
785bool X86FixupVectorConstantsImpl::runOnMachineFunction(MachineFunction &MF) {
790 SM = &ST->getSchedModel();
792 for (MachineBasicBlock &
MBB : MF) {
793 for (MachineInstr &
MI :
MBB) {
794 if (processInstruction(MF,
MBB,
MI)) {
804bool X86FixupVectorConstantsLegacy::runOnMachineFunction(MachineFunction &MF) {
805 X86FixupVectorConstantsImpl Impl;
806 return Impl.runOnMachineFunction(MF);
812 X86FixupVectorConstantsImpl Impl;
813 return Impl.runOnMachineFunction(MF)
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.
Represents analyses that only rely on functions' control flow.
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.
unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
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.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const X86InstrInfo * getInstrInfo() const override
bool hasNoDomainDelayMov() const
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ 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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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 * createX86FixupVectorConstantsLegacyPass()
Machine model for scheduling, bundling, and heuristics.
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)