1 //===- SLPVectorizer.cpp - A bottom up SLP Vectorizer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This pass implements the Bottom Up SLP vectorizer. It detects consecutive
10 // stores that can be put together into vector-stores. Next, it attempts to
11 // construct vectorizable tree using the use-def chains. If a profitable tree
12 // was found, the SLP vectorizer performs vectorization on the tree.
14 // The pass is inspired by the work described in the paper:
15 // "Loop-Aware SLP in GCC" by Ira Rosen, Dorit Nuzman, Ayal Zaks.
17 //===----------------------------------------------------------------------===//
18 #define SV_NAME "slp-vectorizer"
19 #define DEBUG_TYPE SV_NAME
22 #include "llvm/Transforms/Vectorize.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/ScalarEvolution.h"
25 #include "llvm/Analysis/TargetTransformInfo.h"
26 #include "llvm/Analysis/Verifier.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/IR/Value.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/raw_ostream.h"
41 SLPCostThreshold("slp-threshold", cl::init(1), cl::Hidden,
42 cl::desc("Only vectorize trees if the gain is above this "
43 "number. (gain = -cost of vectorization)"));
46 /// The SLPVectorizer Pass.
47 struct SLPVectorizer : public BasicBlockPass {
48 typedef std::map<Value*, BoUpSLP::StoreList> StoreListMap;
50 /// Pass identification, replacement for typeid
53 explicit SLPVectorizer() : BasicBlockPass(ID) {
54 initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
59 TargetTransformInfo *TTI;
62 /// \brief Collect memory references and sort them according to their base
63 /// object. We sort the stores to their base objects to reduce the cost of the
64 /// quadratic search on the stores. TODO: We can further reduce this cost
65 /// if we flush the chain creation every time we run into a memory barrier.
66 bool CollectStores(BasicBlock *BB, BoUpSLP &R) {
67 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
68 StoreInst *SI = dyn_cast<StoreInst>(it);
72 // Check that the pointer points to scalars.
73 if (SI->getValueOperand()->getType()->isAggregateType())
76 // Find the base of the GEP.
77 Value *Ptr = SI->getPointerOperand();
78 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
79 Ptr = GEP->getPointerOperand();
81 // Save the store locations.
82 StoreRefs[Ptr].push_back(SI);
87 bool RollStoreChains(BoUpSLP &R) {
89 // Attempt to sort and vectorize each of the store-groups.
90 for (StoreListMap::iterator it = StoreRefs.begin(), e = StoreRefs.end();
92 if (it->second.size() < 2)
95 DEBUG(dbgs()<<"SLP: Analyzing a store chain of length " <<
96 it->second.size() << ".\n");
98 Changed |= R.vectorizeStores(it->second, -SLPCostThreshold);
103 virtual bool runOnBasicBlock(BasicBlock &BB) {
104 SE = &getAnalysis<ScalarEvolution>();
105 DL = getAnalysisIfAvailable<DataLayout>();
106 TTI = &getAnalysis<TargetTransformInfo>();
107 AA = &getAnalysis<AliasAnalysis>();
110 // Must have DataLayout. We can't require it because some tests run w/o
115 // Use the bollom up slp vectorizer to construct chains that start with
116 // he store instructions.
117 BoUpSLP R(&BB, SE, DL, TTI, AA);
119 if (!CollectStores(&BB, R))
122 bool Changed = RollStoreChains(R);
124 DEBUG(dbgs()<<"SLP: vectorized in \""<<BB.getParent()->getName()<<"\"\n");
125 DEBUG(verifyFunction(*BB.getParent()));
131 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
132 BasicBlockPass::getAnalysisUsage(AU);
133 AU.addRequired<ScalarEvolution>();
134 AU.addRequired<AliasAnalysis>();
135 AU.addRequired<TargetTransformInfo>();
139 StoreListMap StoreRefs;
142 } // end anonymous namespace
144 char SLPVectorizer::ID = 0;
145 static const char lv_name[] = "SLP Vectorizer";
146 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
147 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
148 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
149 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
150 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
151 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
154 Pass *createSLPVectorizerPass() {
155 return new SLPVectorizer();