1*0b57cec5SDimitry Andric //===--- ExpandReductions.cpp - Expand experimental reduction intrinsics --===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This pass implements IR expansion for reduction intrinsics, allowing targets
10e8d8bef9SDimitry Andric // to enable the intrinsics until just before codegen.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #include "llvm/CodeGen/ExpandReductions.h"
15*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
16*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
18*0b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
19*0b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
21*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
22*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
24*0b57cec5SDimitry Andric #include "llvm/Pass.h"
25*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric using namespace llvm;
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric namespace {
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric unsigned getOpcode(Intrinsic::ID ID) {
32*0b57cec5SDimitry Andric   switch (ID) {
33e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fadd:
34*0b57cec5SDimitry Andric     return Instruction::FAdd;
35e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fmul:
36*0b57cec5SDimitry Andric     return Instruction::FMul;
37e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_add:
38*0b57cec5SDimitry Andric     return Instruction::Add;
39e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_mul:
40*0b57cec5SDimitry Andric     return Instruction::Mul;
41e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_and:
42*0b57cec5SDimitry Andric     return Instruction::And;
43e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_or:
44*0b57cec5SDimitry Andric     return Instruction::Or;
45e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_xor:
46*0b57cec5SDimitry Andric     return Instruction::Xor;
47e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_smax:
48e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_smin:
49e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_umax:
50e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_umin:
51*0b57cec5SDimitry Andric     return Instruction::ICmp;
52e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fmax:
53e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fmin:
54*0b57cec5SDimitry Andric     return Instruction::FCmp;
55*0b57cec5SDimitry Andric   default:
56*0b57cec5SDimitry Andric     llvm_unreachable("Unexpected ID");
57*0b57cec5SDimitry Andric   }
58*0b57cec5SDimitry Andric }
59*0b57cec5SDimitry Andric 
60e8d8bef9SDimitry Andric RecurKind getRK(Intrinsic::ID ID) {
61*0b57cec5SDimitry Andric   switch (ID) {
62e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_smax:
63e8d8bef9SDimitry Andric     return RecurKind::SMax;
64e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_smin:
65e8d8bef9SDimitry Andric     return RecurKind::SMin;
66e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_umax:
67e8d8bef9SDimitry Andric     return RecurKind::UMax;
68e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_umin:
69e8d8bef9SDimitry Andric     return RecurKind::UMin;
70e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fmax:
71e8d8bef9SDimitry Andric     return RecurKind::FMax;
72e8d8bef9SDimitry Andric   case Intrinsic::vector_reduce_fmin:
73e8d8bef9SDimitry Andric     return RecurKind::FMin;
74*0b57cec5SDimitry Andric   default:
75e8d8bef9SDimitry Andric     return RecurKind::None;
76*0b57cec5SDimitry Andric   }
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric bool expandReductions(Function &F, const TargetTransformInfo *TTI) {
80*0b57cec5SDimitry Andric   bool Changed = false;
81*0b57cec5SDimitry Andric   SmallVector<IntrinsicInst *, 4> Worklist;
82480093f4SDimitry Andric   for (auto &I : instructions(F)) {
83480093f4SDimitry Andric     if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
84480093f4SDimitry Andric       switch (II->getIntrinsicID()) {
85480093f4SDimitry Andric       default: break;
86e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_fadd:
87e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_fmul:
88e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_add:
89e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_mul:
90e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_and:
91e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_or:
92e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_xor:
93e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_smax:
94e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_smin:
95e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_umax:
96e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_umin:
97e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_fmax:
98e8d8bef9SDimitry Andric       case Intrinsic::vector_reduce_fmin:
99480093f4SDimitry Andric         if (TTI->shouldExpandReduction(II))
100*0b57cec5SDimitry Andric           Worklist.push_back(II);
101*0b57cec5SDimitry Andric 
102480093f4SDimitry Andric         break;
103480093f4SDimitry Andric       }
104480093f4SDimitry Andric     }
105480093f4SDimitry Andric   }
106*0b57cec5SDimitry Andric 
107480093f4SDimitry Andric   for (auto *II : Worklist) {
108*0b57cec5SDimitry Andric     FastMathFlags FMF =
109*0b57cec5SDimitry Andric         isa<FPMathOperator>(II) ? II->getFastMathFlags() : FastMathFlags{};
110*0b57cec5SDimitry Andric     Intrinsic::ID ID = II->getIntrinsicID();
111e8d8bef9SDimitry Andric     RecurKind RK = getRK(ID);
112*0b57cec5SDimitry Andric 
113*0b57cec5SDimitry Andric     Value *Rdx = nullptr;
114*0b57cec5SDimitry Andric     IRBuilder<> Builder(II);
115*0b57cec5SDimitry Andric     IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
116*0b57cec5SDimitry Andric     Builder.setFastMathFlags(FMF);
117*0b57cec5SDimitry Andric     switch (ID) {
118480093f4SDimitry Andric     default: llvm_unreachable("Unexpected intrinsic!");
119e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_fadd:
120e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_fmul: {
121*0b57cec5SDimitry Andric       // FMFs must be attached to the call, otherwise it's an ordered reduction
122*0b57cec5SDimitry Andric       // and it can't be handled by generating a shuffle sequence.
123*0b57cec5SDimitry Andric       Value *Acc = II->getArgOperand(0);
124*0b57cec5SDimitry Andric       Value *Vec = II->getArgOperand(1);
125*0b57cec5SDimitry Andric       if (!FMF.allowReassoc())
126e8d8bef9SDimitry Andric         Rdx = getOrderedReduction(Builder, Acc, Vec, getOpcode(ID), RK);
127*0b57cec5SDimitry Andric       else {
1285ffd83dbSDimitry Andric         if (!isPowerOf2_32(
1295ffd83dbSDimitry Andric                 cast<FixedVectorType>(Vec->getType())->getNumElements()))
130480093f4SDimitry Andric           continue;
131480093f4SDimitry Andric 
132e8d8bef9SDimitry Andric         Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), RK);
133*0b57cec5SDimitry Andric         Rdx = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(ID),
134*0b57cec5SDimitry Andric                                   Acc, Rdx, "bin.rdx");
135*0b57cec5SDimitry Andric       }
136480093f4SDimitry Andric       break;
137480093f4SDimitry Andric     }
138e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_add:
139e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_mul:
140e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_and:
141e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_or:
142e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_xor:
143e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_smax:
144e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_smin:
145e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_umax:
146e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_umin: {
147*0b57cec5SDimitry Andric       Value *Vec = II->getArgOperand(0);
1485ffd83dbSDimitry Andric       if (!isPowerOf2_32(
1495ffd83dbSDimitry Andric               cast<FixedVectorType>(Vec->getType())->getNumElements()))
150*0b57cec5SDimitry Andric         continue;
151480093f4SDimitry Andric 
152e8d8bef9SDimitry Andric       Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), RK);
153e8d8bef9SDimitry Andric       break;
154e8d8bef9SDimitry Andric     }
155e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_fmax:
156e8d8bef9SDimitry Andric     case Intrinsic::vector_reduce_fmin: {
157e8d8bef9SDimitry Andric       // FIXME: We only expand 'fast' reductions here because the underlying
158e8d8bef9SDimitry Andric       //        code in createMinMaxOp() assumes that comparisons use 'fast'
159e8d8bef9SDimitry Andric       //        semantics.
160e8d8bef9SDimitry Andric       Value *Vec = II->getArgOperand(0);
161e8d8bef9SDimitry Andric       if (!isPowerOf2_32(
162e8d8bef9SDimitry Andric               cast<FixedVectorType>(Vec->getType())->getNumElements()) ||
163e8d8bef9SDimitry Andric           !FMF.isFast())
164e8d8bef9SDimitry Andric         continue;
165e8d8bef9SDimitry Andric 
166e8d8bef9SDimitry Andric       Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), RK);
167480093f4SDimitry Andric       break;
168480093f4SDimitry Andric     }
169*0b57cec5SDimitry Andric     }
170*0b57cec5SDimitry Andric     II->replaceAllUsesWith(Rdx);
171*0b57cec5SDimitry Andric     II->eraseFromParent();
172*0b57cec5SDimitry Andric     Changed = true;
173*0b57cec5SDimitry Andric   }
174*0b57cec5SDimitry Andric   return Changed;
175*0b57cec5SDimitry Andric }
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric class ExpandReductions : public FunctionPass {
178*0b57cec5SDimitry Andric public:
179*0b57cec5SDimitry Andric   static char ID;
180*0b57cec5SDimitry Andric   ExpandReductions() : FunctionPass(ID) {
181*0b57cec5SDimitry Andric     initializeExpandReductionsPass(*PassRegistry::getPassRegistry());
182*0b57cec5SDimitry Andric   }
183*0b57cec5SDimitry Andric 
184*0b57cec5SDimitry Andric   bool runOnFunction(Function &F) override {
185*0b57cec5SDimitry Andric     const auto *TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
186*0b57cec5SDimitry Andric     return expandReductions(F, TTI);
187*0b57cec5SDimitry Andric   }
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
190*0b57cec5SDimitry Andric     AU.addRequired<TargetTransformInfoWrapperPass>();
191*0b57cec5SDimitry Andric     AU.setPreservesCFG();
192*0b57cec5SDimitry Andric   }
193*0b57cec5SDimitry Andric };
194*0b57cec5SDimitry Andric }
195*0b57cec5SDimitry Andric 
196*0b57cec5SDimitry Andric char ExpandReductions::ID;
197*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions",
198*0b57cec5SDimitry Andric                       "Expand reduction intrinsics", false, false)
199*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
200*0b57cec5SDimitry Andric INITIALIZE_PASS_END(ExpandReductions, "expand-reductions",
201*0b57cec5SDimitry Andric                     "Expand reduction intrinsics", false, false)
202*0b57cec5SDimitry Andric 
203*0b57cec5SDimitry Andric FunctionPass *llvm::createExpandReductionsPass() {
204*0b57cec5SDimitry Andric   return new ExpandReductions();
205*0b57cec5SDimitry Andric }
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric PreservedAnalyses ExpandReductionsPass::run(Function &F,
208*0b57cec5SDimitry Andric                                             FunctionAnalysisManager &AM) {
209*0b57cec5SDimitry Andric   const auto &TTI = AM.getResult<TargetIRAnalysis>(F);
210*0b57cec5SDimitry Andric   if (!expandReductions(F, &TTI))
211*0b57cec5SDimitry Andric     return PreservedAnalyses::all();
212*0b57cec5SDimitry Andric   PreservedAnalyses PA;
213*0b57cec5SDimitry Andric   PA.preserveSet<CFGAnalyses>();
214*0b57cec5SDimitry Andric   return PA;
215*0b57cec5SDimitry Andric }
216