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/IntrinsicInst.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
42 SLPCostThreshold("slp-threshold", cl::init(0), cl::Hidden,
43 cl::desc("Only vectorize trees if the gain is above this "
44 "number. (gain = -cost of vectorization)"));
47 /// The SLPVectorizer Pass.
48 struct SLPVectorizer : public BasicBlockPass {
49 typedef std::map<Value*, BoUpSLP::StoreList> StoreListMap;
51 /// Pass identification, replacement for typeid
54 explicit SLPVectorizer() : BasicBlockPass(ID) {
55 initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
60 TargetTransformInfo *TTI;
63 /// \brief Collect memory references and sort them according to their base
64 /// object. We sort the stores to their base objects to reduce the cost of the
65 /// quadratic search on the stores. TODO: We can further reduce this cost
66 /// if we flush the chain creation every time we run into a memory barrier.
67 bool collectStores(BasicBlock *BB, BoUpSLP &R) {
68 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
69 StoreInst *SI = dyn_cast<StoreInst>(it);
73 // Check that the pointer points to scalars.
74 if (SI->getValueOperand()->getType()->isAggregateType())
77 // Find the base of the GEP.
78 Value *Ptr = SI->getPointerOperand();
79 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
80 Ptr = GEP->getPointerOperand();
82 // Save the store locations.
83 StoreRefs[Ptr].push_back(SI);
88 bool tryToVectorizePair(BinaryOperator *A, BinaryOperator *B, BoUpSLP &R) {
89 if (!A || !B) return false;
90 BoUpSLP::ValueList VL;
93 int Cost = R.getTreeCost(VL);
94 DEBUG(dbgs()<<"SLP: Cost of pair:" << Cost << ".\n");
95 if (Cost >= -SLPCostThreshold) return false;
96 DEBUG(dbgs()<<"SLP: Vectorizing pair.\n");
101 bool tryToVectorizeCandidate(BinaryOperator *V, BoUpSLP &R) {
102 if (!V) return false;
103 BinaryOperator *A = dyn_cast<BinaryOperator>(V->getOperand(0));
104 BinaryOperator *B = dyn_cast<BinaryOperator>(V->getOperand(1));
105 // Try to vectorize V.
106 if (tryToVectorizePair(A, B, R)) return true;
109 if (B && B->hasOneUse()) {
110 BinaryOperator *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
111 BinaryOperator *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
112 if (tryToVectorizePair(A, B0, R)) {
116 if (tryToVectorizePair(A, B1, R)) {
123 if (A && A->hasOneUse()) {
124 BinaryOperator *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
125 BinaryOperator *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
126 if (tryToVectorizePair(A0, B, R)) {
130 if (tryToVectorizePair(A1, B, R)) {
138 bool vectorizeReductions(BasicBlock *BB, BoUpSLP &R) {
139 bool Changed = false;
140 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
141 if (isa<DbgInfoIntrinsic>(it)) continue;
142 PHINode *P = dyn_cast<PHINode>(it);
143 if (!P) return Changed;
144 // Check that the PHI is a reduction PHI.
145 if (P->getNumIncomingValues() != 2) return Changed;
146 Value *Rdx = (P->getIncomingBlock(0) == BB ? P->getIncomingValue(0) :
147 (P->getIncomingBlock(1) == BB ? P->getIncomingValue(1) : 0));
148 // Check if this is a Binary Operator.
149 BinaryOperator *BI = dyn_cast_or_null<BinaryOperator>(Rdx);
152 Value *Inst = BI->getOperand(0);
153 if (Inst == P) Inst = BI->getOperand(1);
154 Changed |= tryToVectorizeCandidate(dyn_cast<BinaryOperator>(Inst), R);
160 bool rollStoreChains(BoUpSLP &R) {
161 bool Changed = false;
162 // Attempt to sort and vectorize each of the store-groups.
163 for (StoreListMap::iterator it = StoreRefs.begin(), e = StoreRefs.end();
165 if (it->second.size() < 2)
168 DEBUG(dbgs()<<"SLP: Analyzing a store chain of length " <<
169 it->second.size() << ".\n");
171 Changed |= R.vectorizeStores(it->second, -SLPCostThreshold);
176 virtual bool runOnBasicBlock(BasicBlock &BB) {
177 SE = &getAnalysis<ScalarEvolution>();
178 DL = getAnalysisIfAvailable<DataLayout>();
179 TTI = &getAnalysis<TargetTransformInfo>();
180 AA = &getAnalysis<AliasAnalysis>();
183 // Must have DataLayout. We can't require it because some tests run w/o
188 // Use the bollom up slp vectorizer to construct chains that start with
189 // he store instructions.
190 BoUpSLP R(&BB, SE, DL, TTI, AA);
192 bool Changed = vectorizeReductions(&BB, R);
194 if (!collectStores(&BB, R))
197 if (rollStoreChains(R)) {
198 DEBUG(dbgs()<<"SLP: vectorized in \""<<BB.getParent()->getName()<<"\"\n");
199 DEBUG(verifyFunction(*BB.getParent()));
206 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
207 BasicBlockPass::getAnalysisUsage(AU);
208 AU.addRequired<ScalarEvolution>();
209 AU.addRequired<AliasAnalysis>();
210 AU.addRequired<TargetTransformInfo>();
214 StoreListMap StoreRefs;
217 } // end anonymous namespace
219 char SLPVectorizer::ID = 0;
220 static const char lv_name[] = "SLP Vectorizer";
221 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
222 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
223 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
224 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
225 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
226 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
229 Pass *createSLPVectorizerPass() {
230 return new SLPVectorizer();