22#include "llvm/IR/IntrinsicsX86.h"
29#define DEBUG_TYPE "x86-partial-reduction"
49 return "X86 Partial Reduction";
59 return new X86PartialReduction();
62char X86PartialReduction::ID = 0;
65 "X86 Partial Reduction",
false,
false)
70 if (!ST->hasVNNI() && !ST->hasAVXVNNI())
81 if (Cast->getParent() ==
Mul->getParent() &&
82 (Cast->getOpcode() == Instruction::SExt ||
83 Cast->getOpcode() == Instruction::ZExt) &&
84 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 8)
103 bool ReduceInOneBB) {
126 if (ReduceInOneBB && matchVPDPBUSDPattern(
ST,
Mul,
DL))
133 if (
ST->hasSSE41()) {
145 auto CanShrinkOp = [&](
Value *
Op) {
149 (Cast->getOpcode() == Instruction::SExt ||
150 Cast->getOpcode() == Instruction::ZExt) &&
151 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 16)
177 if (!CanShrinkOp(
LHS) && !CanShrinkOp(
RHS))
183 unsigned NumElts = MulTy->getNumElements();
188 SmallVector<int, 16> EvenMask(NumElts / 2);
189 SmallVector<int, 16> OddMask(NumElts / 2);
190 for (
int i = 0, e = NumElts / 2; i !=
e; ++i) {
192 OddMask[i] = i * 2 + 1;
197 Value *EvenElts = Builder.CreateShuffleVector(NewMul, NewMul, EvenMask);
198 Value *OddElts = Builder.CreateShuffleVector(NewMul, NewMul, OddMask);
199 Value *
MAdd = Builder.CreateAdd(EvenElts, OddElts);
203 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
205 Value *
Concat = Builder.CreateShuffleVector(MAdd, Zero, ConcatMask);
213bool X86PartialReduction::trySADReplacement(Instruction *
Op) {
224 LHS =
Op->getOperand(0);
240 if (!
Sub ||
Sub->getOpcode() != Instruction::Sub)
249 return ZExt->getOperand(0);
255 Value *Op0 = getZeroExtendedVal(
Sub->getOperand(0));
256 Value *Op1 = getZeroExtendedVal(
Sub->getOperand(1));
263 unsigned NumElts = OpTy->getNumElements();
265 unsigned IntrinsicNumElts;
267 if (
ST->hasBWI() && NumElts >= 64) {
268 IID = Intrinsic::x86_avx512_psad_bw_512;
269 IntrinsicNumElts = 64;
270 }
else if (
ST->hasAVX2() && NumElts >= 32) {
271 IID = Intrinsic::x86_avx2_psad_bw;
272 IntrinsicNumElts = 32;
274 IID = Intrinsic::x86_sse2_psad_bw;
275 IntrinsicNumElts = 16;
283 for (
unsigned i = 0; i != NumElts; ++i)
285 for (
unsigned i = NumElts; i != 16; ++i)
286 ConcatMask[i] = (i % NumElts) + NumElts;
289 Op0 = Builder.CreateShuffleVector(Op0, Zero, ConcatMask);
290 Op1 = Builder.CreateShuffleVector(Op1, Zero, ConcatMask);
298 assert(NumElts % IntrinsicNumElts == 0 &&
"Unexpected number of elements!");
299 unsigned NumSplits = NumElts / IntrinsicNumElts;
303 for (
unsigned i = 0; i != NumSplits; ++i) {
305 std::iota(ExtractMask.begin(), ExtractMask.end(), i * IntrinsicNumElts);
306 Value *ExtractOp0 = Builder.CreateShuffleVector(Op0, Op0, ExtractMask);
307 Value *ExtractOp1 = Builder.CreateShuffleVector(Op1, Op0, ExtractMask);
308 Ops[i] = Builder.CreateCall(PSADBWFn, {ExtractOp0, ExtractOp1});
309 Ops[i] = Builder.CreateBitCast(
Ops[i], I32Ty);
313 unsigned Stages =
Log2_32(NumSplits);
314 for (
unsigned s = Stages; s > 0; --s) {
315 unsigned NumConcatElts =
317 for (
unsigned i = 0; i != 1U << (s - 1); ++i) {
319 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
320 Ops[i] = Builder.CreateShuffleVector(
Ops[i*2],
Ops[i*2+1], ConcatMask);
329 Ops[0] = Builder.CreateShuffleVector(
Ops[0],
Ops[0], ArrayRef<int>{0, 1});
330 }
else if (NumElts >= 8) {
334 for (
unsigned i = 0; i != SubElts; ++i)
336 for (
unsigned i = SubElts; i != NumElts; ++i)
337 ConcatMask[i] = (i % SubElts) + SubElts;
340 Ops[0] = Builder.CreateShuffleVector(
Ops[0], Zero, ConcatMask);
343 Op->replaceAllUsesWith(
Ops[0]);
344 Op->eraseFromParent();
352 bool &ReduceInOneBB) {
353 ReduceInOneBB =
true;
356 if (!Index || !Index->isNullValue())
360 if (!BO || BO->getOpcode() != Instruction::Add || !BO->hasOneUse())
363 ReduceInOneBB =
false;
371 unsigned Stages =
Log2_32(NumElems);
372 for (
unsigned i = 0; i != Stages; ++i) {
374 if (!BO || BO->getOpcode() != Instruction::Add)
377 ReduceInOneBB =
false;
381 if (i != 0 && !BO->hasNUses(2))
397 if (!Shuffle || Shuffle->getOperand(0) !=
Op)
401 unsigned MaskEnd = 1 << i;
402 for (
unsigned Index = 0; Index < MaskEnd; ++Index)
403 if (Shuffle->getMaskValue(Index) != (
int)(MaskEnd + Index))
407 return const_cast<Value *
>(
Op);
415 if (!Phi->hasOneUse())
422 while (U->hasOneUse() && U->getOpcode() == BO->
getOpcode())
437 while (!Worklist.
empty()) {
439 if (!Visited.
insert(V).second)
445 if (!PN->hasNUses(PN == Root ? 2 : 1))
455 if (BO->getOpcode() == Instruction::Add) {
457 if (BO->hasNUses(BO == Root ? 2 : 1)) {
464 if (BO->hasNUses(BO == Root ? 3 : 2)) {
466 for (
auto *U : BO->users())
488 if (!V->hasNUses(
I == Root ? 2 : 1))
497bool X86PartialReduction::runOnFunction(Function &
F) {
501 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
505 auto &
TM = TPC->getTM<X86TargetMachine>();
506 ST =
TM.getSubtargetImpl(
F);
508 DL = &
F.getDataLayout();
510 bool MadeChange =
false;
524 SmallVector<Instruction *, 8> Leaves;
527 for (Instruction *
I : Leaves) {
528 if (tryMAddReplacement(
I, ReduceInOneBB)) {
535 if (
I != Root && trySADReplacement(
I))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static SymbolRef::Type getType(const Symbol *Sym)
Target-Independent Code Generator Pass Configuration Options pass.
static constexpr int Concat[]
static bool isReachableFromPHI(PHINode *Phi, BinaryOperator *BO)
if(isa< SExtInst >(LHS)) std auto IsFreeTruncation
static Value * matchAddReduction(const ExtractElementInst &EE, bool &ReduceInOneBB)
static void collectLeaves(Value *Root, SmallVectorImpl< Instruction * > &Leaves)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
BinaryOps getOpcode() const
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
FunctionPass class - This class is used to implement most global optimizations.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
bool hasOneUse() const
Return true if there is exactly one use of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
const ParentTy * getParent() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
@ SPF_ABS
Floating point maxnum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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...
FunctionPass * createX86PartialReductionPass()
This pass optimizes arithmetic based on knowledge that is only used by a reduction sequence and is th...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.