LLVM  6.0.0svn
ExpandReductions.cpp
Go to the documentation of this file.
1 //===--- ExpandReductions.cpp - Expand experimental reduction intrinsics --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass implements IR expansion for reduction intrinsics, allowing targets
11 // to enable the experimental intrinsics until just before codegen.
12 //
13 //===----------------------------------------------------------------------===//
14 
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
26 
27 using namespace llvm;
28 
29 namespace {
30 
31 unsigned getOpcode(Intrinsic::ID ID) {
32  switch (ID) {
33  case Intrinsic::experimental_vector_reduce_fadd:
34  return Instruction::FAdd;
35  case Intrinsic::experimental_vector_reduce_fmul:
36  return Instruction::FMul;
37  case Intrinsic::experimental_vector_reduce_add:
38  return Instruction::Add;
39  case Intrinsic::experimental_vector_reduce_mul:
40  return Instruction::Mul;
41  case Intrinsic::experimental_vector_reduce_and:
42  return Instruction::And;
43  case Intrinsic::experimental_vector_reduce_or:
44  return Instruction::Or;
45  case Intrinsic::experimental_vector_reduce_xor:
46  return Instruction::Xor;
47  case Intrinsic::experimental_vector_reduce_smax:
48  case Intrinsic::experimental_vector_reduce_smin:
49  case Intrinsic::experimental_vector_reduce_umax:
50  case Intrinsic::experimental_vector_reduce_umin:
51  return Instruction::ICmp;
52  case Intrinsic::experimental_vector_reduce_fmax:
53  case Intrinsic::experimental_vector_reduce_fmin:
54  return Instruction::FCmp;
55  default:
56  llvm_unreachable("Unexpected ID");
57  }
58 }
59 
61  switch (ID) {
62  case Intrinsic::experimental_vector_reduce_smax:
64  case Intrinsic::experimental_vector_reduce_smin:
66  case Intrinsic::experimental_vector_reduce_umax:
68  case Intrinsic::experimental_vector_reduce_umin:
70  case Intrinsic::experimental_vector_reduce_fmax:
72  case Intrinsic::experimental_vector_reduce_fmin:
74  default:
76  }
77 }
78 
79 bool expandReductions(Function &F, const TargetTransformInfo *TTI) {
80  bool Changed = false;
82  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
83  if (auto II = dyn_cast<IntrinsicInst>(&*I))
84  Worklist.push_back(II);
85 
86  for (auto *II : Worklist) {
87  IRBuilder<> Builder(II);
88  Value *Vec = nullptr;
89  auto ID = II->getIntrinsicID();
91  switch (ID) {
92  case Intrinsic::experimental_vector_reduce_fadd:
93  case Intrinsic::experimental_vector_reduce_fmul:
94  // FMFs must be attached to the call, otherwise it's an ordered reduction
95  // and it can't be handled by generating this shuffle sequence.
96  // TODO: Implement scalarization of ordered reductions here for targets
97  // without native support.
98  if (!II->getFastMathFlags().isFast())
99  continue;
100  Vec = II->getArgOperand(1);
101  break;
102  case Intrinsic::experimental_vector_reduce_add:
103  case Intrinsic::experimental_vector_reduce_mul:
104  case Intrinsic::experimental_vector_reduce_and:
105  case Intrinsic::experimental_vector_reduce_or:
106  case Intrinsic::experimental_vector_reduce_xor:
107  case Intrinsic::experimental_vector_reduce_smax:
108  case Intrinsic::experimental_vector_reduce_smin:
109  case Intrinsic::experimental_vector_reduce_umax:
110  case Intrinsic::experimental_vector_reduce_umin:
111  case Intrinsic::experimental_vector_reduce_fmax:
112  case Intrinsic::experimental_vector_reduce_fmin:
113  Vec = II->getArgOperand(0);
114  MRK = getMRK(ID);
115  break;
116  default:
117  continue;
118  }
119  if (!TTI->shouldExpandReduction(II))
120  continue;
121  auto Rdx = getShuffleReduction(Builder, Vec, getOpcode(ID), MRK);
122  II->replaceAllUsesWith(Rdx);
123  II->eraseFromParent();
124  Changed = true;
125  }
126  return Changed;
127 }
128 
129 class ExpandReductions : public FunctionPass {
130 public:
131  static char ID;
132  ExpandReductions() : FunctionPass(ID) {
134  }
135 
136  bool runOnFunction(Function &F) override {
137  const auto *TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
138  return expandReductions(F, TTI);
139  }
140 
141  void getAnalysisUsage(AnalysisUsage &AU) const override {
143  AU.setPreservesCFG();
144  }
145 };
146 }
147 
149 INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions",
150  "Expand reduction intrinsics", false, false)
153  "Expand reduction intrinsics", false, false)
154 
156  return new ExpandReductions();
157 }
158 
161  const auto &TTI = AM.getResult<TargetIRAnalysis>(F);
162  if (!expandReductions(F, &TTI))
163  return PreservedAnalyses::all();
165  PA.preserveSet<CFGAnalyses>();
166  return PA;
167 }
FunctionPass * createExpandReductionsPass()
This pass expands the experimental reduction intrinsics into sequences of shuffles.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Analysis pass providing the TargetTransformInfo.
expand Expand reduction intrinsics
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
void initializeExpandReductionsPass(PassRegistry &)
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:132
Value * getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value *> RedOps=ArrayRef< Value *>())
Generates a vector reduction using shufflevectors to reduce the value.
Definition: LoopUtils.cpp:1349
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:668
static bool runOnFunction(Function &F, bool PostInlining)
Wrapper pass for TargetTransformInfo.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Straight line strength reduction
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static Expected< BitVector > expand(StringRef S, StringRef Original)
Definition: GlobPattern.cpp:28
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
expand reductions
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
Module.h This file contains the declarations for the Module class.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:285
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:114
bool shouldExpandReduction(const IntrinsicInst *II) const
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:189
#define I(x, y, z)
Definition: MD5.cpp:58
INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions", "Expand reduction intrinsics", false, false) INITIALIZE_PASS_END(ExpandReductions
LLVM Value Representation.
Definition: Value.h:73
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
This pass exposes codegen information to IR-level passes.