37 switch (
II->getIntrinsicID()) {
40 case Intrinsic::vector_reduce_fadd:
41 case Intrinsic::vector_reduce_fmul:
42 case Intrinsic::vector_reduce_add:
43 case Intrinsic::vector_reduce_mul:
44 case Intrinsic::vector_reduce_and:
45 case Intrinsic::vector_reduce_or:
46 case Intrinsic::vector_reduce_xor:
47 case Intrinsic::vector_reduce_smax:
48 case Intrinsic::vector_reduce_smin:
49 case Intrinsic::vector_reduce_umax:
50 case Intrinsic::vector_reduce_umin:
51 case Intrinsic::vector_reduce_fmax:
52 case Intrinsic::vector_reduce_fmin:
53 case Intrinsic::vector_reduce_fmaximum:
54 case Intrinsic::vector_reduce_fminimum:
55 case Intrinsic::vector_reduce_fmaximumnum:
56 case Intrinsic::vector_reduce_fminimumnum: {
58 if (
TTI->shouldExpandReduction(
II))
66 for (
auto *
II : Worklist) {
71 TTI->getPreferredExpandedReductionShuffle(
II);
76 Builder.setFastMathFlags(FMF);
80 case Intrinsic::vector_reduce_fadd:
81 case Intrinsic::vector_reduce_fmul: {
84 Value *Acc =
II->getArgOperand(0);
85 Value *Vec =
II->getArgOperand(1);
103 case Intrinsic::vector_reduce_and:
104 case Intrinsic::vector_reduce_or: {
112 Value *Vec =
II->getArgOperand(0);
114 unsigned NumElts = FTy->getNumElements();
118 if (FTy->getElementType() == Builder.getInt1Ty()) {
119 Rdx = Builder.CreateBitCast(Vec, Builder.getIntNTy(NumElts));
120 if (ID == Intrinsic::vector_reduce_and) {
121 Rdx = Builder.CreateICmpEQ(
124 assert(ID == Intrinsic::vector_reduce_or &&
"Expected or reduction.");
125 Rdx = Builder.CreateIsNotNull(Rdx);
133 case Intrinsic::vector_reduce_add:
134 case Intrinsic::vector_reduce_mul:
135 case Intrinsic::vector_reduce_xor:
136 case Intrinsic::vector_reduce_smax:
137 case Intrinsic::vector_reduce_smin:
138 case Intrinsic::vector_reduce_umax:
139 case Intrinsic::vector_reduce_umin: {
140 Value *Vec =
II->getArgOperand(0);
154 case Intrinsic::vector_reduce_fmax:
155 case Intrinsic::vector_reduce_fmin: {
158 Value *Vec =
II->getArgOperand(0);
167 case Intrinsic::vector_reduce_fmaximum:
168 case Intrinsic::vector_reduce_fminimum:
169 case Intrinsic::vector_reduce_fmaximumnum:
170 case Intrinsic::vector_reduce_fminimumnum: {
171 Value *Vec =
II->getArgOperand(0);
180 II->replaceAllUsesWith(Rdx);
181 II->eraseFromParent();
190 ExpandReductions() : FunctionPass(ID) {}
193 const auto *
TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
194 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
195 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
196 auto *DT = DTWP ? &DTWP->getDomTree() :
nullptr;
197 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
198 return expandReductions(
F,
TTI, DT, LI);
201 void getAnalysisUsage(AnalysisUsage &AU)
const override {
209char ExpandReductions::ID;
211 "Expand reduction intrinsics",
false,
false)
217 return new ExpandReductions();
225 if (!expandReductions(
F, &
TTI, DT, LI))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
static bool runOnFunction(Function &F, bool PostInlining)
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Convenience struct for specifying and reasoning about fast-math flags.
bool allowReassoc() const
Flag queries.
FunctionPass class - This class is used to implement most global optimizations.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Analysis pass that exposes the LoopInfo for a function.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserve()
Mark an analysis 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.
Analysis pass providing the TargetTransformInfo.
The instances of the Type class are immutable: once they are created, they are never changed.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Value * getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags FMF)
Given information about an @llvm.vector.reduce.
LLVM_ABI unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM_ABI Value * getShuffleReduction(IRBuilderBase &Builder, Value *Src, unsigned Op, TargetTransformInfo::ReductionShuffle RS, RecurKind MinMaxKind=RecurKind::None)
Generates a vector reduction using shufflevectors to reduce the value.
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...
RecurKind
These are the kinds of recurrences that we support.
LLVM_ABI FunctionPass * createExpandReductionsPass()
This pass expands the reduction intrinsics into sequences of shuffles.
LLVM_ABI Value * expandReductionViaLoop(IRBuilderBase &Builder, Value *Vec, unsigned RdxOpcode, Value *Acc, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Expand a scalable vector reduction into a runtime loop that applies RdxOpcode element by element,...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI RecurKind getMinMaxReductionRecurKind(Intrinsic::ID RdxID)
Returns the recurence kind used when expanding a min/max reduction.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI Value * getOrderedReduction(IRBuilderBase &Builder, Value *Acc, Value *Src, unsigned Op, RecurKind MinMaxKind=RecurKind::None)
Generates an ordered vector reduction using extracts to reduce the value.