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())
76 if (isa<SExtInst>(
LHS))
80 if (
auto *Cast = dyn_cast<CastInst>(
Op)) {
82 (Cast->getOpcode() == Instruction::SExt ||
83 Cast->getOpcode() == Instruction::ZExt) &&
84 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 8)
88 return isa<Constant>(
Op);
103 bool ReduceInOneBB) {
108 if (cast<FixedVectorType>(
Op->getType())->getNumElements() < 8)
112 if (!cast<VectorType>(
Op->getType())->getElementType()->isIntegerTy(32))
115 auto *
Mul = dyn_cast<BinaryOperator>(
Op);
126 if (ReduceInOneBB && matchVPDPBUSDPattern(
ST,
Mul,
DL))
133 if (
ST->hasSSE41()) {
145 auto CanShrinkOp = [&](
Value *
Op) {
147 if (
auto *Cast = dyn_cast<CastInst>(
Op)) {
149 (Cast->getOpcode() == Instruction::SExt ||
150 Cast->getOpcode() == Instruction::ZExt) &&
151 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 16)
155 return isa<Constant>(
Op);
165 if (
auto *BO = dyn_cast<BinaryOperator>(
Op)) {
177 if (!CanShrinkOp(
LHS) && !CanShrinkOp(
RHS))
182 auto *MulTy = cast<FixedVectorType>(
Op->getType());
183 unsigned NumElts = MulTy->getNumElements();
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) {
219 if (!cast<VectorType>(
Op->getType())->getElementType()->isIntegerTy(32))
223 if (
match(
Op, PatternMatch::m_Intrinsic<Intrinsic::abs>())) {
224 LHS =
Op->getOperand(0);
227 auto *
SI = dyn_cast<SelectInst>(
Op);
239 auto *
Sub = dyn_cast<BinaryOperator>(
LHS);
240 if (!
Sub ||
Sub->getOpcode() != Instruction::Sub)
245 if (
auto *ZExt = dyn_cast<ZExtInst>(
Op))
246 if (cast<VectorType>(ZExt->getOperand(0)->getType())
249 return ZExt->getOperand(0);
255 Value *Op0 = getZeroExtendedVal(
Sub->getOperand(0));
256 Value *Op1 = getZeroExtendedVal(
Sub->getOperand(1));
262 auto *OpTy = cast<FixedVectorType>(
Op->getType());
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 =
316 cast<FixedVectorType>(Ops[0]->
getType())->getNumElements() * 2;
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);
326 NumElts = cast<FixedVectorType>(OpTy)->getNumElements();
329 Ops[0] = Builder.CreateShuffleVector(Ops[0], Ops[0],
ArrayRef<int>{0, 1});
330 }
else if (NumElts >= 8) {
333 cast<FixedVectorType>(Ops[0]->
getType())->getNumElements();
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;
365 unsigned NumElems = cast<FixedVectorType>(BO->getType())->getNumElements();
371 unsigned Stages =
Log2_32(NumElems);
372 for (
unsigned i = 0; i != Stages; ++i) {
373 const auto *BO = dyn_cast<BinaryOperator>(
Op);
374 if (!BO || BO->getOpcode() != Instruction::Add)
377 ReduceInOneBB =
false;
381 if (i != 0 && !BO->hasNUses(2))
387 auto *Shuffle = dyn_cast<ShuffleVectorInst>(
LHS);
391 Shuffle = dyn_cast<ShuffleVectorInst>(
RHS);
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())
418 Instruction *U = cast<Instruction>(*Phi->user_begin());
422 while (U->hasOneUse() && U->getOpcode() == BO->
getOpcode())
423 U = cast<Instruction>(*U->user_begin());
437 while (!Worklist.
empty()) {
439 if (!Visited.
insert(V).second)
442 if (
auto *PN = dyn_cast<PHINode>(V)) {
445 if (!PN->hasNUses(PN == Root ? 2 : 1))
454 if (
auto *BO = dyn_cast<BinaryOperator>(V)) {
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())
467 if (
auto *
P = dyn_cast<PHINode>(U))
487 if (
auto *
I = dyn_cast<Instruction>(V)) {
488 if (!V->hasNUses(
I == Root ? 2 : 1))
497bool X86PartialReduction::runOnFunction(
Function &
F) {
501 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
506 ST =
TM.getSubtargetImpl(
F);
508 DL = &
F.getDataLayout();
510 bool MadeChange =
false;
513 auto *EE = dyn_cast<ExtractElementInst>(&
I);
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...
#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)
BinaryOperator const DataLayout * DL
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:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
BinaryOps getOpcode() const
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
This class represents an Operation in the Expression.
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.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
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.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
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)
This is an optimization pass for GlobalISel generic memory operations.
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...
FunctionPass * createX86PartialReductionPass()
This pass optimizes arithmetic based on knowledge that is only used by a reduction sequence and is th...
@ 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.
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.