LLVM  8.0.1
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) {
34  return Instruction::FAdd;
36  return Instruction::FMul;
38  return Instruction::Add;
40  return Instruction::Mul;
42  return Instruction::And;
44  return Instruction::Or;
46  return Instruction::Xor;
51  return Instruction::ICmp;
54  return Instruction::FCmp;
55  default:
56  llvm_unreachable("Unexpected ID");
57  }
58 }
59 
61  switch (ID) {
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  bool IsOrdered = false;
89  Value *Acc = nullptr;
90  Value *Vec = nullptr;
91  auto ID = II->getIntrinsicID();
93  switch (ID) {
96  // FMFs must be attached to the call, otherwise it's an ordered reduction
97  // and it can't be handled by generating a shuffle sequence.
98  if (!II->getFastMathFlags().isFast())
99  IsOrdered = true;
100  Acc = II->getArgOperand(0);
101  Vec = II->getArgOperand(1);
102  break;
114  Vec = II->getArgOperand(0);
115  MRK = getMRK(ID);
116  break;
117  default:
118  continue;
119  }
120  if (!TTI->shouldExpandReduction(II))
121  continue;
122  Value *Rdx =
123  IsOrdered ? getOrderedReduction(Builder, Acc, Vec, getOpcode(ID), MRK)
124  : getShuffleReduction(Builder, Vec, getOpcode(ID), MRK);
125  II->replaceAllUsesWith(Rdx);
126  II->eraseFromParent();
127  Changed = true;
128  }
129  return Changed;
130 }
131 
132 class ExpandReductions : public FunctionPass {
133 public:
134  static char ID;
135  ExpandReductions() : FunctionPass(ID) {
137  }
138 
139  bool runOnFunction(Function &F) override {
140  const auto *TTI =&getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
141  return expandReductions(F, TTI);
142  }
143 
144  void getAnalysisUsage(AnalysisUsage &AU) const override {
146  AU.setPreservesCFG();
147  }
148 };
149 }
150 
152 INITIALIZE_PASS_BEGIN(ExpandReductions, "expand-reductions",
153  "Expand reduction intrinsics", false, false)
156  "Expand reduction intrinsics", false, false)
157 
159  return new ExpandReductions();
160 }
161 
164  const auto &TTI = AM.getResult<TargetIRAnalysis>(F);
165  if (!expandReductions(F, &TTI))
166  return PreservedAnalyses::all();
168  PA.preserveSet<CFGAnalyses>();
169  return PA;
170 }
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:770
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Analysis pass providing the TargetTransformInfo.
expand Expand reduction intrinsics
F(f)
Value * getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value *> RedOps=None)
Generates a vector reduction using shufflevectors to reduce the value.
Definition: LoopUtils.cpp:759
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
void initializeExpandReductionsPass(PassRegistry &)
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:132
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:197
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
Value * getOrderedReduction(IRBuilder<> &Builder, Value *Acc, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value *> RedOps=None)
Generates an ordered vector reduction using extracts to reduce the value.
Definition: LoopUtils.cpp:728
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
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:154
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:160
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:847
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:286
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
bool shouldExpandReduction(const IntrinsicInst *II) const
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:190
#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.