SLPVectorizer: add initial support for reduction variable vectorization.
[oota-llvm.git] / lib / Transforms / Vectorize / SLPVectorizer.cpp
1 //===- SLPVectorizer.cpp - A bottom up SLP Vectorizer ---------------------===//
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 // 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.
13 //
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.
16 //
17 //===----------------------------------------------------------------------===//
18 #define SV_NAME "slp-vectorizer"
19 #define DEBUG_TYPE SV_NAME
20
21 #include "VecUtils.h"
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"
37 #include <map>
38
39 using namespace llvm;
40
41 static cl::opt<int>
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)"));
45 namespace {
46
47 /// The SLPVectorizer Pass.
48 struct SLPVectorizer : public BasicBlockPass {
49   typedef std::map<Value*, BoUpSLP::StoreList> StoreListMap;
50
51   /// Pass identification, replacement for typeid
52   static char ID;
53
54   explicit SLPVectorizer() : BasicBlockPass(ID) {
55     initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
56   }
57
58   ScalarEvolution *SE;
59   DataLayout *DL;
60   TargetTransformInfo *TTI;
61   AliasAnalysis *AA;
62
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);
70       if (!SI)
71         continue;
72
73       // Check that the pointer points to scalars.
74       if (SI->getValueOperand()->getType()->isAggregateType())
75         return false;
76
77       // Find the base of the GEP.
78       Value *Ptr = SI->getPointerOperand();
79       if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
80         Ptr = GEP->getPointerOperand();
81
82       // Save the store locations.
83       StoreRefs[Ptr].push_back(SI);
84     }
85     return true;
86   }
87
88   bool tryToVectorizePair(BinaryOperator *A, BinaryOperator *B,  BoUpSLP &R) {
89     if (!A || !B) return false;
90     BoUpSLP::ValueList VL;
91     VL.push_back(A);
92     VL.push_back(B);
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");
97     R.vectorizeArith(VL);
98     return true;
99   }
100
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;
107
108     // Try to skip B.
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)) {
113         B->moveBefore(V);
114         return true;
115       }
116       if (tryToVectorizePair(A, B1, R)) {
117         B->moveBefore(V);
118         return true;
119       }
120     }
121
122     // Try to slip A.
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)) {
127         A->moveBefore(V);
128         return true;
129       }
130       if (tryToVectorizePair(A1, B, R)) {
131         A->moveBefore(V);
132         return true;
133       }
134     }
135     return 0;
136   }
137
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);
150       if (!BI) continue;
151
152       Value *Inst = BI->getOperand(0);
153       if (Inst == P) Inst = BI->getOperand(1);
154       Changed |= tryToVectorizeCandidate(dyn_cast<BinaryOperator>(Inst), R);
155     }
156
157     return Changed;
158   }
159
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();
164          it != e; ++it) {
165       if (it->second.size() < 2)
166         continue;
167
168       DEBUG(dbgs()<<"SLP: Analyzing a store chain of length " <<
169             it->second.size() << ".\n");
170
171       Changed |= R.vectorizeStores(it->second, -SLPCostThreshold);
172     }
173     return Changed;
174   }
175
176   virtual bool runOnBasicBlock(BasicBlock &BB) {
177     SE = &getAnalysis<ScalarEvolution>();
178     DL = getAnalysisIfAvailable<DataLayout>();
179     TTI = &getAnalysis<TargetTransformInfo>();
180     AA = &getAnalysis<AliasAnalysis>();
181     StoreRefs.clear();
182
183     // Must have DataLayout. We can't require it because some tests run w/o
184     // triple.
185     if (!DL)
186       return false;
187
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);
191
192     bool Changed = vectorizeReductions(&BB, R);
193
194     if (!collectStores(&BB, R))
195       return Changed;
196
197     if (rollStoreChains(R)) {
198       DEBUG(dbgs()<<"SLP: vectorized in \""<<BB.getParent()->getName()<<"\"\n");
199       DEBUG(verifyFunction(*BB.getParent()));
200       Changed |= true;
201     }
202
203     return Changed;
204   }
205
206   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
207     BasicBlockPass::getAnalysisUsage(AU);
208     AU.addRequired<ScalarEvolution>();
209     AU.addRequired<AliasAnalysis>();
210     AU.addRequired<TargetTransformInfo>();
211   }
212
213 private:
214   StoreListMap StoreRefs;
215 };
216
217 } // end anonymous namespace
218
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)
227
228 namespace llvm {
229   Pass *createSLPVectorizerPass() {
230     return new SLPVectorizer();
231   }
232 }
233