LLVM: lib/CodeGen/ExpandReductions.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
24
25using namespace llvm;
26
27namespace {
28
34 switch (II->getIntrinsicID()) {
35 default: break;
36 case Intrinsic::vector_reduce_fadd:
37 case Intrinsic::vector_reduce_fmul:
38 case Intrinsic::vector_reduce_add:
39 case Intrinsic::vector_reduce_mul:
40 case Intrinsic::vector_reduce_and:
41 case Intrinsic::vector_reduce_or:
42 case Intrinsic::vector_reduce_xor:
43 case Intrinsic::vector_reduce_smax:
44 case Intrinsic::vector_reduce_smin:
45 case Intrinsic::vector_reduce_umax:
46 case Intrinsic::vector_reduce_umin:
47 case Intrinsic::vector_reduce_fmax:
48 case Intrinsic::vector_reduce_fmin:
49 if (TTI->shouldExpandReduction(II))
51
52 break;
53 }
54 }
55 }
56
57 for (auto *II : Worklist) {
63 TTI->getPreferredExpandedReductionShuffle(II);
64
65 Value *Rdx = nullptr;
68 Builder.setFastMathFlags(FMF);
69 switch (ID) {
71 case Intrinsic::vector_reduce_fadd:
72 case Intrinsic::vector_reduce_fmul: {
73
74
75 Value *Acc = II->getArgOperand(0);
76 Value *Vec = II->getArgOperand(1);
80 else {
83 continue;
86 "bin.rdx");
87 }
88 break;
89 }
90 case Intrinsic::vector_reduce_and:
91 case Intrinsic::vector_reduce_or: {
92
93
94
95
96
97
98
99 Value *Vec = II->getArgOperand(0);
101 unsigned NumElts = FTy->getNumElements();
103 continue;
104
105 if (FTy->getElementType() == Builder.getInt1Ty()) {
106 Rdx = Builder.CreateBitCast(Vec, Builder.getIntNTy(NumElts));
107 if (ID == Intrinsic::vector_reduce_and) {
108 Rdx = Builder.CreateICmpEQ(
110 } else {
111 assert(ID == Intrinsic::vector_reduce_or && "Expected or reduction.");
112 Rdx = Builder.CreateIsNotNull(Rdx);
113 }
114 break;
115 }
118 break;
119 }
120 case Intrinsic::vector_reduce_add:
121 case Intrinsic::vector_reduce_mul:
122 case Intrinsic::vector_reduce_xor:
123 case Intrinsic::vector_reduce_smax:
124 case Intrinsic::vector_reduce_smin:
125 case Intrinsic::vector_reduce_umax:
126 case Intrinsic::vector_reduce_umin: {
127 Value *Vec = II->getArgOperand(0);
130 continue;
133 break;
134 }
135 case Intrinsic::vector_reduce_fmax:
136 case Intrinsic::vector_reduce_fmin: {
137
138
139 Value *Vec = II->getArgOperand(0);
143 continue;
146 break;
147 }
148 }
149 II->replaceAllUsesWith(Rdx);
150 II->eraseFromParent();
152 }
154}
155
156class ExpandReductions : public FunctionPass {
157public:
158 static char ID;
159 ExpandReductions() : FunctionPass(ID) {
161 }
162
164 const auto *TTI =&getAnalysis().getTTI(F);
165 return expandReductions(F, TTI);
166 }
167
168 void getAnalysisUsage(AnalysisUsage &AU) const override {
169 AU.addRequired();
171 }
172};
173}
174
175char ExpandReductions::ID;
177 "Expand reduction intrinsics", false, false)
181
183 return new ExpandReductions();
184}
185
189 if (!expandReductions(F, &TTI))
193 return PA;
194}
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)
This pass exposes codegen information to IR-level passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition ExpandReductions.cpp:186
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...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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 & 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.
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
LLVM_ABI unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
LLVM_ABI void initializeExpandReductionsPass(PassRegistry &)
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 - 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.
Definition ExpandReductions.cpp:182
decltype(auto) cast(const From &Val)
cast - 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.