1*0a6a1f1dSLionel Sambuc //===--- Scalarizer.cpp - Scalarize vector operations ---------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This pass converts vector operations into scalar operations, in order
11*0a6a1f1dSLionel Sambuc // to expose optimization opportunities on the individual scalar operations.
12*0a6a1f1dSLionel Sambuc // It is mainly intended for targets that do not have vector units, but it
13*0a6a1f1dSLionel Sambuc // may also be useful for revectorizing code to different vector widths.
14*0a6a1f1dSLionel Sambuc //
15*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc #include "llvm/ADT/STLExtras.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/IR/IRBuilder.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/IR/InstVisitor.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/Pass.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/CommandLine.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Transforms/Scalar.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Transforms/Utils/BasicBlockUtils.h"
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc using namespace llvm;
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "scalarizer"
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc namespace {
30*0a6a1f1dSLionel Sambuc // Used to store the scattered form of a vector.
31*0a6a1f1dSLionel Sambuc typedef SmallVector<Value *, 8> ValueVector;
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc // Used to map a vector Value to its scattered form.  We use std::map
34*0a6a1f1dSLionel Sambuc // because we want iterators to persist across insertion and because the
35*0a6a1f1dSLionel Sambuc // values are relatively large.
36*0a6a1f1dSLionel Sambuc typedef std::map<Value *, ValueVector> ScatterMap;
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc // Lists Instructions that have been replaced with scalar implementations,
39*0a6a1f1dSLionel Sambuc // along with a pointer to their scattered forms.
40*0a6a1f1dSLionel Sambuc typedef SmallVector<std::pair<Instruction *, ValueVector *>, 16> GatherList;
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc // Provides a very limited vector-like interface for lazily accessing one
43*0a6a1f1dSLionel Sambuc // component of a scattered vector or vector pointer.
44*0a6a1f1dSLionel Sambuc class Scatterer {
45*0a6a1f1dSLionel Sambuc public:
Scatterer()46*0a6a1f1dSLionel Sambuc   Scatterer() {}
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc   // Scatter V into Size components.  If new instructions are needed,
49*0a6a1f1dSLionel Sambuc   // insert them before BBI in BB.  If Cache is nonnull, use it to cache
50*0a6a1f1dSLionel Sambuc   // the results.
51*0a6a1f1dSLionel Sambuc   Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v,
52*0a6a1f1dSLionel Sambuc             ValueVector *cachePtr = nullptr);
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc   // Return component I, creating a new Value for it if necessary.
55*0a6a1f1dSLionel Sambuc   Value *operator[](unsigned I);
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc   // Return the number of components.
size() const58*0a6a1f1dSLionel Sambuc   unsigned size() const { return Size; }
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc private:
61*0a6a1f1dSLionel Sambuc   BasicBlock *BB;
62*0a6a1f1dSLionel Sambuc   BasicBlock::iterator BBI;
63*0a6a1f1dSLionel Sambuc   Value *V;
64*0a6a1f1dSLionel Sambuc   ValueVector *CachePtr;
65*0a6a1f1dSLionel Sambuc   PointerType *PtrTy;
66*0a6a1f1dSLionel Sambuc   ValueVector Tmp;
67*0a6a1f1dSLionel Sambuc   unsigned Size;
68*0a6a1f1dSLionel Sambuc };
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc // FCmpSpliiter(FCI)(Builder, X, Y, Name) uses Builder to create an FCmp
71*0a6a1f1dSLionel Sambuc // called Name that compares X and Y in the same way as FCI.
72*0a6a1f1dSLionel Sambuc struct FCmpSplitter {
FCmpSplitter__anon6086aeee0111::FCmpSplitter73*0a6a1f1dSLionel Sambuc   FCmpSplitter(FCmpInst &fci) : FCI(fci) {}
operator ()__anon6086aeee0111::FCmpSplitter74*0a6a1f1dSLionel Sambuc   Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1,
75*0a6a1f1dSLionel Sambuc                     const Twine &Name) const {
76*0a6a1f1dSLionel Sambuc     return Builder.CreateFCmp(FCI.getPredicate(), Op0, Op1, Name);
77*0a6a1f1dSLionel Sambuc   }
78*0a6a1f1dSLionel Sambuc   FCmpInst &FCI;
79*0a6a1f1dSLionel Sambuc };
80*0a6a1f1dSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc // ICmpSpliiter(ICI)(Builder, X, Y, Name) uses Builder to create an ICmp
82*0a6a1f1dSLionel Sambuc // called Name that compares X and Y in the same way as ICI.
83*0a6a1f1dSLionel Sambuc struct ICmpSplitter {
ICmpSplitter__anon6086aeee0111::ICmpSplitter84*0a6a1f1dSLionel Sambuc   ICmpSplitter(ICmpInst &ici) : ICI(ici) {}
operator ()__anon6086aeee0111::ICmpSplitter85*0a6a1f1dSLionel Sambuc   Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1,
86*0a6a1f1dSLionel Sambuc                     const Twine &Name) const {
87*0a6a1f1dSLionel Sambuc     return Builder.CreateICmp(ICI.getPredicate(), Op0, Op1, Name);
88*0a6a1f1dSLionel Sambuc   }
89*0a6a1f1dSLionel Sambuc   ICmpInst &ICI;
90*0a6a1f1dSLionel Sambuc };
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc // BinarySpliiter(BO)(Builder, X, Y, Name) uses Builder to create
93*0a6a1f1dSLionel Sambuc // a binary operator like BO called Name with operands X and Y.
94*0a6a1f1dSLionel Sambuc struct BinarySplitter {
BinarySplitter__anon6086aeee0111::BinarySplitter95*0a6a1f1dSLionel Sambuc   BinarySplitter(BinaryOperator &bo) : BO(bo) {}
operator ()__anon6086aeee0111::BinarySplitter96*0a6a1f1dSLionel Sambuc   Value *operator()(IRBuilder<> &Builder, Value *Op0, Value *Op1,
97*0a6a1f1dSLionel Sambuc                     const Twine &Name) const {
98*0a6a1f1dSLionel Sambuc     return Builder.CreateBinOp(BO.getOpcode(), Op0, Op1, Name);
99*0a6a1f1dSLionel Sambuc   }
100*0a6a1f1dSLionel Sambuc   BinaryOperator &BO;
101*0a6a1f1dSLionel Sambuc };
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc // Information about a load or store that we're scalarizing.
104*0a6a1f1dSLionel Sambuc struct VectorLayout {
VectorLayout__anon6086aeee0111::VectorLayout105*0a6a1f1dSLionel Sambuc   VectorLayout() : VecTy(nullptr), ElemTy(nullptr), VecAlign(0), ElemSize(0) {}
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc   // Return the alignment of element I.
getElemAlign__anon6086aeee0111::VectorLayout108*0a6a1f1dSLionel Sambuc   uint64_t getElemAlign(unsigned I) {
109*0a6a1f1dSLionel Sambuc     return MinAlign(VecAlign, I * ElemSize);
110*0a6a1f1dSLionel Sambuc   }
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc   // The type of the vector.
113*0a6a1f1dSLionel Sambuc   VectorType *VecTy;
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc   // The type of each element.
116*0a6a1f1dSLionel Sambuc   Type *ElemTy;
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc   // The alignment of the vector.
119*0a6a1f1dSLionel Sambuc   uint64_t VecAlign;
120*0a6a1f1dSLionel Sambuc 
121*0a6a1f1dSLionel Sambuc   // The size of each element.
122*0a6a1f1dSLionel Sambuc   uint64_t ElemSize;
123*0a6a1f1dSLionel Sambuc };
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc class Scalarizer : public FunctionPass,
126*0a6a1f1dSLionel Sambuc                    public InstVisitor<Scalarizer, bool> {
127*0a6a1f1dSLionel Sambuc public:
128*0a6a1f1dSLionel Sambuc   static char ID;
129*0a6a1f1dSLionel Sambuc 
Scalarizer()130*0a6a1f1dSLionel Sambuc   Scalarizer() :
131*0a6a1f1dSLionel Sambuc     FunctionPass(ID) {
132*0a6a1f1dSLionel Sambuc     initializeScalarizerPass(*PassRegistry::getPassRegistry());
133*0a6a1f1dSLionel Sambuc   }
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc   bool doInitialization(Module &M) override;
136*0a6a1f1dSLionel Sambuc   bool runOnFunction(Function &F) override;
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc   // InstVisitor methods.  They return true if the instruction was scalarized,
139*0a6a1f1dSLionel Sambuc   // false if nothing changed.
visitInstruction(Instruction &)140*0a6a1f1dSLionel Sambuc   bool visitInstruction(Instruction &) { return false; }
141*0a6a1f1dSLionel Sambuc   bool visitSelectInst(SelectInst &SI);
142*0a6a1f1dSLionel Sambuc   bool visitICmpInst(ICmpInst &);
143*0a6a1f1dSLionel Sambuc   bool visitFCmpInst(FCmpInst &);
144*0a6a1f1dSLionel Sambuc   bool visitBinaryOperator(BinaryOperator &);
145*0a6a1f1dSLionel Sambuc   bool visitGetElementPtrInst(GetElementPtrInst &);
146*0a6a1f1dSLionel Sambuc   bool visitCastInst(CastInst &);
147*0a6a1f1dSLionel Sambuc   bool visitBitCastInst(BitCastInst &);
148*0a6a1f1dSLionel Sambuc   bool visitShuffleVectorInst(ShuffleVectorInst &);
149*0a6a1f1dSLionel Sambuc   bool visitPHINode(PHINode &);
150*0a6a1f1dSLionel Sambuc   bool visitLoadInst(LoadInst &);
151*0a6a1f1dSLionel Sambuc   bool visitStoreInst(StoreInst &);
152*0a6a1f1dSLionel Sambuc 
registerOptions()153*0a6a1f1dSLionel Sambuc   static void registerOptions() {
154*0a6a1f1dSLionel Sambuc     // This is disabled by default because having separate loads and stores
155*0a6a1f1dSLionel Sambuc     // makes it more likely that the -combiner-alias-analysis limits will be
156*0a6a1f1dSLionel Sambuc     // reached.
157*0a6a1f1dSLionel Sambuc     OptionRegistry::registerOption<bool, Scalarizer,
158*0a6a1f1dSLionel Sambuc                                  &Scalarizer::ScalarizeLoadStore>(
159*0a6a1f1dSLionel Sambuc         "scalarize-load-store",
160*0a6a1f1dSLionel Sambuc         "Allow the scalarizer pass to scalarize loads and store", false);
161*0a6a1f1dSLionel Sambuc   }
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc private:
164*0a6a1f1dSLionel Sambuc   Scatterer scatter(Instruction *, Value *);
165*0a6a1f1dSLionel Sambuc   void gather(Instruction *, const ValueVector &);
166*0a6a1f1dSLionel Sambuc   bool canTransferMetadata(unsigned Kind);
167*0a6a1f1dSLionel Sambuc   void transferMetadata(Instruction *, const ValueVector &);
168*0a6a1f1dSLionel Sambuc   bool getVectorLayout(Type *, unsigned, VectorLayout &);
169*0a6a1f1dSLionel Sambuc   bool finish();
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc   template<typename T> bool splitBinary(Instruction &, const T &);
172*0a6a1f1dSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc   ScatterMap Scattered;
174*0a6a1f1dSLionel Sambuc   GatherList Gathered;
175*0a6a1f1dSLionel Sambuc   unsigned ParallelLoopAccessMDKind;
176*0a6a1f1dSLionel Sambuc   const DataLayout *DL;
177*0a6a1f1dSLionel Sambuc   bool ScalarizeLoadStore;
178*0a6a1f1dSLionel Sambuc };
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc char Scalarizer::ID = 0;
181*0a6a1f1dSLionel Sambuc } // end anonymous namespace
182*0a6a1f1dSLionel Sambuc 
183*0a6a1f1dSLionel Sambuc INITIALIZE_PASS_WITH_OPTIONS(Scalarizer, "scalarizer",
184*0a6a1f1dSLionel Sambuc                              "Scalarize vector operations", false, false)
185*0a6a1f1dSLionel Sambuc 
Scatterer(BasicBlock * bb,BasicBlock::iterator bbi,Value * v,ValueVector * cachePtr)186*0a6a1f1dSLionel Sambuc Scatterer::Scatterer(BasicBlock *bb, BasicBlock::iterator bbi, Value *v,
187*0a6a1f1dSLionel Sambuc                      ValueVector *cachePtr)
188*0a6a1f1dSLionel Sambuc   : BB(bb), BBI(bbi), V(v), CachePtr(cachePtr) {
189*0a6a1f1dSLionel Sambuc   Type *Ty = V->getType();
190*0a6a1f1dSLionel Sambuc   PtrTy = dyn_cast<PointerType>(Ty);
191*0a6a1f1dSLionel Sambuc   if (PtrTy)
192*0a6a1f1dSLionel Sambuc     Ty = PtrTy->getElementType();
193*0a6a1f1dSLionel Sambuc   Size = Ty->getVectorNumElements();
194*0a6a1f1dSLionel Sambuc   if (!CachePtr)
195*0a6a1f1dSLionel Sambuc     Tmp.resize(Size, nullptr);
196*0a6a1f1dSLionel Sambuc   else if (CachePtr->empty())
197*0a6a1f1dSLionel Sambuc     CachePtr->resize(Size, nullptr);
198*0a6a1f1dSLionel Sambuc   else
199*0a6a1f1dSLionel Sambuc     assert(Size == CachePtr->size() && "Inconsistent vector sizes");
200*0a6a1f1dSLionel Sambuc }
201*0a6a1f1dSLionel Sambuc 
202*0a6a1f1dSLionel Sambuc // Return component I, creating a new Value for it if necessary.
operator [](unsigned I)203*0a6a1f1dSLionel Sambuc Value *Scatterer::operator[](unsigned I) {
204*0a6a1f1dSLionel Sambuc   ValueVector &CV = (CachePtr ? *CachePtr : Tmp);
205*0a6a1f1dSLionel Sambuc   // Try to reuse a previous value.
206*0a6a1f1dSLionel Sambuc   if (CV[I])
207*0a6a1f1dSLionel Sambuc     return CV[I];
208*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(BB, BBI);
209*0a6a1f1dSLionel Sambuc   if (PtrTy) {
210*0a6a1f1dSLionel Sambuc     if (!CV[0]) {
211*0a6a1f1dSLionel Sambuc       Type *Ty =
212*0a6a1f1dSLionel Sambuc         PointerType::get(PtrTy->getElementType()->getVectorElementType(),
213*0a6a1f1dSLionel Sambuc                          PtrTy->getAddressSpace());
214*0a6a1f1dSLionel Sambuc       CV[0] = Builder.CreateBitCast(V, Ty, V->getName() + ".i0");
215*0a6a1f1dSLionel Sambuc     }
216*0a6a1f1dSLionel Sambuc     if (I != 0)
217*0a6a1f1dSLionel Sambuc       CV[I] = Builder.CreateConstGEP1_32(CV[0], I,
218*0a6a1f1dSLionel Sambuc                                          V->getName() + ".i" + Twine(I));
219*0a6a1f1dSLionel Sambuc   } else {
220*0a6a1f1dSLionel Sambuc     // Search through a chain of InsertElementInsts looking for element I.
221*0a6a1f1dSLionel Sambuc     // Record other elements in the cache.  The new V is still suitable
222*0a6a1f1dSLionel Sambuc     // for all uncached indices.
223*0a6a1f1dSLionel Sambuc     for (;;) {
224*0a6a1f1dSLionel Sambuc       InsertElementInst *Insert = dyn_cast<InsertElementInst>(V);
225*0a6a1f1dSLionel Sambuc       if (!Insert)
226*0a6a1f1dSLionel Sambuc         break;
227*0a6a1f1dSLionel Sambuc       ConstantInt *Idx = dyn_cast<ConstantInt>(Insert->getOperand(2));
228*0a6a1f1dSLionel Sambuc       if (!Idx)
229*0a6a1f1dSLionel Sambuc         break;
230*0a6a1f1dSLionel Sambuc       unsigned J = Idx->getZExtValue();
231*0a6a1f1dSLionel Sambuc       CV[J] = Insert->getOperand(1);
232*0a6a1f1dSLionel Sambuc       V = Insert->getOperand(0);
233*0a6a1f1dSLionel Sambuc       if (I == J)
234*0a6a1f1dSLionel Sambuc         return CV[J];
235*0a6a1f1dSLionel Sambuc     }
236*0a6a1f1dSLionel Sambuc     CV[I] = Builder.CreateExtractElement(V, Builder.getInt32(I),
237*0a6a1f1dSLionel Sambuc                                          V->getName() + ".i" + Twine(I));
238*0a6a1f1dSLionel Sambuc   }
239*0a6a1f1dSLionel Sambuc   return CV[I];
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc 
doInitialization(Module & M)242*0a6a1f1dSLionel Sambuc bool Scalarizer::doInitialization(Module &M) {
243*0a6a1f1dSLionel Sambuc   ParallelLoopAccessMDKind =
244*0a6a1f1dSLionel Sambuc       M.getContext().getMDKindID("llvm.mem.parallel_loop_access");
245*0a6a1f1dSLionel Sambuc   ScalarizeLoadStore =
246*0a6a1f1dSLionel Sambuc       M.getContext().getOption<bool, Scalarizer, &Scalarizer::ScalarizeLoadStore>();
247*0a6a1f1dSLionel Sambuc   return false;
248*0a6a1f1dSLionel Sambuc }
249*0a6a1f1dSLionel Sambuc 
runOnFunction(Function & F)250*0a6a1f1dSLionel Sambuc bool Scalarizer::runOnFunction(Function &F) {
251*0a6a1f1dSLionel Sambuc   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
252*0a6a1f1dSLionel Sambuc   DL = DLP ? &DLP->getDataLayout() : nullptr;
253*0a6a1f1dSLionel Sambuc   for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI) {
254*0a6a1f1dSLionel Sambuc     BasicBlock *BB = BBI;
255*0a6a1f1dSLionel Sambuc     for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
256*0a6a1f1dSLionel Sambuc       Instruction *I = II;
257*0a6a1f1dSLionel Sambuc       bool Done = visit(I);
258*0a6a1f1dSLionel Sambuc       ++II;
259*0a6a1f1dSLionel Sambuc       if (Done && I->getType()->isVoidTy())
260*0a6a1f1dSLionel Sambuc         I->eraseFromParent();
261*0a6a1f1dSLionel Sambuc     }
262*0a6a1f1dSLionel Sambuc   }
263*0a6a1f1dSLionel Sambuc   return finish();
264*0a6a1f1dSLionel Sambuc }
265*0a6a1f1dSLionel Sambuc 
266*0a6a1f1dSLionel Sambuc // Return a scattered form of V that can be accessed by Point.  V must be a
267*0a6a1f1dSLionel Sambuc // vector or a pointer to a vector.
scatter(Instruction * Point,Value * V)268*0a6a1f1dSLionel Sambuc Scatterer Scalarizer::scatter(Instruction *Point, Value *V) {
269*0a6a1f1dSLionel Sambuc   if (Argument *VArg = dyn_cast<Argument>(V)) {
270*0a6a1f1dSLionel Sambuc     // Put the scattered form of arguments in the entry block,
271*0a6a1f1dSLionel Sambuc     // so that it can be used everywhere.
272*0a6a1f1dSLionel Sambuc     Function *F = VArg->getParent();
273*0a6a1f1dSLionel Sambuc     BasicBlock *BB = &F->getEntryBlock();
274*0a6a1f1dSLionel Sambuc     return Scatterer(BB, BB->begin(), V, &Scattered[V]);
275*0a6a1f1dSLionel Sambuc   }
276*0a6a1f1dSLionel Sambuc   if (Instruction *VOp = dyn_cast<Instruction>(V)) {
277*0a6a1f1dSLionel Sambuc     // Put the scattered form of an instruction directly after the
278*0a6a1f1dSLionel Sambuc     // instruction.
279*0a6a1f1dSLionel Sambuc     BasicBlock *BB = VOp->getParent();
280*0a6a1f1dSLionel Sambuc     return Scatterer(BB, std::next(BasicBlock::iterator(VOp)),
281*0a6a1f1dSLionel Sambuc                      V, &Scattered[V]);
282*0a6a1f1dSLionel Sambuc   }
283*0a6a1f1dSLionel Sambuc   // In the fallback case, just put the scattered before Point and
284*0a6a1f1dSLionel Sambuc   // keep the result local to Point.
285*0a6a1f1dSLionel Sambuc   return Scatterer(Point->getParent(), Point, V);
286*0a6a1f1dSLionel Sambuc }
287*0a6a1f1dSLionel Sambuc 
288*0a6a1f1dSLionel Sambuc // Replace Op with the gathered form of the components in CV.  Defer the
289*0a6a1f1dSLionel Sambuc // deletion of Op and creation of the gathered form to the end of the pass,
290*0a6a1f1dSLionel Sambuc // so that we can avoid creating the gathered form if all uses of Op are
291*0a6a1f1dSLionel Sambuc // replaced with uses of CV.
gather(Instruction * Op,const ValueVector & CV)292*0a6a1f1dSLionel Sambuc void Scalarizer::gather(Instruction *Op, const ValueVector &CV) {
293*0a6a1f1dSLionel Sambuc   // Since we're not deleting Op yet, stub out its operands, so that it
294*0a6a1f1dSLionel Sambuc   // doesn't make anything live unnecessarily.
295*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I)
296*0a6a1f1dSLionel Sambuc     Op->setOperand(I, UndefValue::get(Op->getOperand(I)->getType()));
297*0a6a1f1dSLionel Sambuc 
298*0a6a1f1dSLionel Sambuc   transferMetadata(Op, CV);
299*0a6a1f1dSLionel Sambuc 
300*0a6a1f1dSLionel Sambuc   // If we already have a scattered form of Op (created from ExtractElements
301*0a6a1f1dSLionel Sambuc   // of Op itself), replace them with the new form.
302*0a6a1f1dSLionel Sambuc   ValueVector &SV = Scattered[Op];
303*0a6a1f1dSLionel Sambuc   if (!SV.empty()) {
304*0a6a1f1dSLionel Sambuc     for (unsigned I = 0, E = SV.size(); I != E; ++I) {
305*0a6a1f1dSLionel Sambuc       Instruction *Old = cast<Instruction>(SV[I]);
306*0a6a1f1dSLionel Sambuc       CV[I]->takeName(Old);
307*0a6a1f1dSLionel Sambuc       Old->replaceAllUsesWith(CV[I]);
308*0a6a1f1dSLionel Sambuc       Old->eraseFromParent();
309*0a6a1f1dSLionel Sambuc     }
310*0a6a1f1dSLionel Sambuc   }
311*0a6a1f1dSLionel Sambuc   SV = CV;
312*0a6a1f1dSLionel Sambuc   Gathered.push_back(GatherList::value_type(Op, &SV));
313*0a6a1f1dSLionel Sambuc }
314*0a6a1f1dSLionel Sambuc 
315*0a6a1f1dSLionel Sambuc // Return true if it is safe to transfer the given metadata tag from
316*0a6a1f1dSLionel Sambuc // vector to scalar instructions.
canTransferMetadata(unsigned Tag)317*0a6a1f1dSLionel Sambuc bool Scalarizer::canTransferMetadata(unsigned Tag) {
318*0a6a1f1dSLionel Sambuc   return (Tag == LLVMContext::MD_tbaa
319*0a6a1f1dSLionel Sambuc           || Tag == LLVMContext::MD_fpmath
320*0a6a1f1dSLionel Sambuc           || Tag == LLVMContext::MD_tbaa_struct
321*0a6a1f1dSLionel Sambuc           || Tag == LLVMContext::MD_invariant_load
322*0a6a1f1dSLionel Sambuc           || Tag == LLVMContext::MD_alias_scope
323*0a6a1f1dSLionel Sambuc           || Tag == LLVMContext::MD_noalias
324*0a6a1f1dSLionel Sambuc           || Tag == ParallelLoopAccessMDKind);
325*0a6a1f1dSLionel Sambuc }
326*0a6a1f1dSLionel Sambuc 
327*0a6a1f1dSLionel Sambuc // Transfer metadata from Op to the instructions in CV if it is known
328*0a6a1f1dSLionel Sambuc // to be safe to do so.
transferMetadata(Instruction * Op,const ValueVector & CV)329*0a6a1f1dSLionel Sambuc void Scalarizer::transferMetadata(Instruction *Op, const ValueVector &CV) {
330*0a6a1f1dSLionel Sambuc   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
331*0a6a1f1dSLionel Sambuc   Op->getAllMetadataOtherThanDebugLoc(MDs);
332*0a6a1f1dSLionel Sambuc   for (unsigned I = 0, E = CV.size(); I != E; ++I) {
333*0a6a1f1dSLionel Sambuc     if (Instruction *New = dyn_cast<Instruction>(CV[I])) {
334*0a6a1f1dSLionel Sambuc       for (SmallVectorImpl<std::pair<unsigned, MDNode *>>::iterator
335*0a6a1f1dSLionel Sambuc                MI = MDs.begin(),
336*0a6a1f1dSLionel Sambuc                ME = MDs.end();
337*0a6a1f1dSLionel Sambuc            MI != ME; ++MI)
338*0a6a1f1dSLionel Sambuc         if (canTransferMetadata(MI->first))
339*0a6a1f1dSLionel Sambuc           New->setMetadata(MI->first, MI->second);
340*0a6a1f1dSLionel Sambuc       New->setDebugLoc(Op->getDebugLoc());
341*0a6a1f1dSLionel Sambuc     }
342*0a6a1f1dSLionel Sambuc   }
343*0a6a1f1dSLionel Sambuc }
344*0a6a1f1dSLionel Sambuc 
345*0a6a1f1dSLionel Sambuc // Try to fill in Layout from Ty, returning true on success.  Alignment is
346*0a6a1f1dSLionel Sambuc // the alignment of the vector, or 0 if the ABI default should be used.
getVectorLayout(Type * Ty,unsigned Alignment,VectorLayout & Layout)347*0a6a1f1dSLionel Sambuc bool Scalarizer::getVectorLayout(Type *Ty, unsigned Alignment,
348*0a6a1f1dSLionel Sambuc                                  VectorLayout &Layout) {
349*0a6a1f1dSLionel Sambuc   if (!DL)
350*0a6a1f1dSLionel Sambuc     return false;
351*0a6a1f1dSLionel Sambuc 
352*0a6a1f1dSLionel Sambuc   // Make sure we're dealing with a vector.
353*0a6a1f1dSLionel Sambuc   Layout.VecTy = dyn_cast<VectorType>(Ty);
354*0a6a1f1dSLionel Sambuc   if (!Layout.VecTy)
355*0a6a1f1dSLionel Sambuc     return false;
356*0a6a1f1dSLionel Sambuc 
357*0a6a1f1dSLionel Sambuc   // Check that we're dealing with full-byte elements.
358*0a6a1f1dSLionel Sambuc   Layout.ElemTy = Layout.VecTy->getElementType();
359*0a6a1f1dSLionel Sambuc   if (DL->getTypeSizeInBits(Layout.ElemTy) !=
360*0a6a1f1dSLionel Sambuc       DL->getTypeStoreSizeInBits(Layout.ElemTy))
361*0a6a1f1dSLionel Sambuc     return false;
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc   if (Alignment)
364*0a6a1f1dSLionel Sambuc     Layout.VecAlign = Alignment;
365*0a6a1f1dSLionel Sambuc   else
366*0a6a1f1dSLionel Sambuc     Layout.VecAlign = DL->getABITypeAlignment(Layout.VecTy);
367*0a6a1f1dSLionel Sambuc   Layout.ElemSize = DL->getTypeStoreSize(Layout.ElemTy);
368*0a6a1f1dSLionel Sambuc   return true;
369*0a6a1f1dSLionel Sambuc }
370*0a6a1f1dSLionel Sambuc 
371*0a6a1f1dSLionel Sambuc // Scalarize two-operand instruction I, using Split(Builder, X, Y, Name)
372*0a6a1f1dSLionel Sambuc // to create an instruction like I with operands X and Y and name Name.
373*0a6a1f1dSLionel Sambuc template<typename Splitter>
splitBinary(Instruction & I,const Splitter & Split)374*0a6a1f1dSLionel Sambuc bool Scalarizer::splitBinary(Instruction &I, const Splitter &Split) {
375*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(I.getType());
376*0a6a1f1dSLionel Sambuc   if (!VT)
377*0a6a1f1dSLionel Sambuc     return false;
378*0a6a1f1dSLionel Sambuc 
379*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
380*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(I.getParent(), &I);
381*0a6a1f1dSLionel Sambuc   Scatterer Op0 = scatter(&I, I.getOperand(0));
382*0a6a1f1dSLionel Sambuc   Scatterer Op1 = scatter(&I, I.getOperand(1));
383*0a6a1f1dSLionel Sambuc   assert(Op0.size() == NumElems && "Mismatched binary operation");
384*0a6a1f1dSLionel Sambuc   assert(Op1.size() == NumElems && "Mismatched binary operation");
385*0a6a1f1dSLionel Sambuc   ValueVector Res;
386*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
387*0a6a1f1dSLionel Sambuc   for (unsigned Elem = 0; Elem < NumElems; ++Elem)
388*0a6a1f1dSLionel Sambuc     Res[Elem] = Split(Builder, Op0[Elem], Op1[Elem],
389*0a6a1f1dSLionel Sambuc                       I.getName() + ".i" + Twine(Elem));
390*0a6a1f1dSLionel Sambuc   gather(&I, Res);
391*0a6a1f1dSLionel Sambuc   return true;
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc 
visitSelectInst(SelectInst & SI)394*0a6a1f1dSLionel Sambuc bool Scalarizer::visitSelectInst(SelectInst &SI) {
395*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(SI.getType());
396*0a6a1f1dSLionel Sambuc   if (!VT)
397*0a6a1f1dSLionel Sambuc     return false;
398*0a6a1f1dSLionel Sambuc 
399*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
400*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(SI.getParent(), &SI);
401*0a6a1f1dSLionel Sambuc   Scatterer Op1 = scatter(&SI, SI.getOperand(1));
402*0a6a1f1dSLionel Sambuc   Scatterer Op2 = scatter(&SI, SI.getOperand(2));
403*0a6a1f1dSLionel Sambuc   assert(Op1.size() == NumElems && "Mismatched select");
404*0a6a1f1dSLionel Sambuc   assert(Op2.size() == NumElems && "Mismatched select");
405*0a6a1f1dSLionel Sambuc   ValueVector Res;
406*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
407*0a6a1f1dSLionel Sambuc 
408*0a6a1f1dSLionel Sambuc   if (SI.getOperand(0)->getType()->isVectorTy()) {
409*0a6a1f1dSLionel Sambuc     Scatterer Op0 = scatter(&SI, SI.getOperand(0));
410*0a6a1f1dSLionel Sambuc     assert(Op0.size() == NumElems && "Mismatched select");
411*0a6a1f1dSLionel Sambuc     for (unsigned I = 0; I < NumElems; ++I)
412*0a6a1f1dSLionel Sambuc       Res[I] = Builder.CreateSelect(Op0[I], Op1[I], Op2[I],
413*0a6a1f1dSLionel Sambuc                                     SI.getName() + ".i" + Twine(I));
414*0a6a1f1dSLionel Sambuc   } else {
415*0a6a1f1dSLionel Sambuc     Value *Op0 = SI.getOperand(0);
416*0a6a1f1dSLionel Sambuc     for (unsigned I = 0; I < NumElems; ++I)
417*0a6a1f1dSLionel Sambuc       Res[I] = Builder.CreateSelect(Op0, Op1[I], Op2[I],
418*0a6a1f1dSLionel Sambuc                                     SI.getName() + ".i" + Twine(I));
419*0a6a1f1dSLionel Sambuc   }
420*0a6a1f1dSLionel Sambuc   gather(&SI, Res);
421*0a6a1f1dSLionel Sambuc   return true;
422*0a6a1f1dSLionel Sambuc }
423*0a6a1f1dSLionel Sambuc 
visitICmpInst(ICmpInst & ICI)424*0a6a1f1dSLionel Sambuc bool Scalarizer::visitICmpInst(ICmpInst &ICI) {
425*0a6a1f1dSLionel Sambuc   return splitBinary(ICI, ICmpSplitter(ICI));
426*0a6a1f1dSLionel Sambuc }
427*0a6a1f1dSLionel Sambuc 
visitFCmpInst(FCmpInst & FCI)428*0a6a1f1dSLionel Sambuc bool Scalarizer::visitFCmpInst(FCmpInst &FCI) {
429*0a6a1f1dSLionel Sambuc   return splitBinary(FCI, FCmpSplitter(FCI));
430*0a6a1f1dSLionel Sambuc }
431*0a6a1f1dSLionel Sambuc 
visitBinaryOperator(BinaryOperator & BO)432*0a6a1f1dSLionel Sambuc bool Scalarizer::visitBinaryOperator(BinaryOperator &BO) {
433*0a6a1f1dSLionel Sambuc   return splitBinary(BO, BinarySplitter(BO));
434*0a6a1f1dSLionel Sambuc }
435*0a6a1f1dSLionel Sambuc 
visitGetElementPtrInst(GetElementPtrInst & GEPI)436*0a6a1f1dSLionel Sambuc bool Scalarizer::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
437*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(GEPI.getType());
438*0a6a1f1dSLionel Sambuc   if (!VT)
439*0a6a1f1dSLionel Sambuc     return false;
440*0a6a1f1dSLionel Sambuc 
441*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(GEPI.getParent(), &GEPI);
442*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
443*0a6a1f1dSLionel Sambuc   unsigned NumIndices = GEPI.getNumIndices();
444*0a6a1f1dSLionel Sambuc 
445*0a6a1f1dSLionel Sambuc   Scatterer Base = scatter(&GEPI, GEPI.getOperand(0));
446*0a6a1f1dSLionel Sambuc 
447*0a6a1f1dSLionel Sambuc   SmallVector<Scatterer, 8> Ops;
448*0a6a1f1dSLionel Sambuc   Ops.resize(NumIndices);
449*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumIndices; ++I)
450*0a6a1f1dSLionel Sambuc     Ops[I] = scatter(&GEPI, GEPI.getOperand(I + 1));
451*0a6a1f1dSLionel Sambuc 
452*0a6a1f1dSLionel Sambuc   ValueVector Res;
453*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
454*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I) {
455*0a6a1f1dSLionel Sambuc     SmallVector<Value *, 8> Indices;
456*0a6a1f1dSLionel Sambuc     Indices.resize(NumIndices);
457*0a6a1f1dSLionel Sambuc     for (unsigned J = 0; J < NumIndices; ++J)
458*0a6a1f1dSLionel Sambuc       Indices[J] = Ops[J][I];
459*0a6a1f1dSLionel Sambuc     Res[I] = Builder.CreateGEP(Base[I], Indices,
460*0a6a1f1dSLionel Sambuc                                GEPI.getName() + ".i" + Twine(I));
461*0a6a1f1dSLionel Sambuc     if (GEPI.isInBounds())
462*0a6a1f1dSLionel Sambuc       if (GetElementPtrInst *NewGEPI = dyn_cast<GetElementPtrInst>(Res[I]))
463*0a6a1f1dSLionel Sambuc         NewGEPI->setIsInBounds();
464*0a6a1f1dSLionel Sambuc   }
465*0a6a1f1dSLionel Sambuc   gather(&GEPI, Res);
466*0a6a1f1dSLionel Sambuc   return true;
467*0a6a1f1dSLionel Sambuc }
468*0a6a1f1dSLionel Sambuc 
visitCastInst(CastInst & CI)469*0a6a1f1dSLionel Sambuc bool Scalarizer::visitCastInst(CastInst &CI) {
470*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(CI.getDestTy());
471*0a6a1f1dSLionel Sambuc   if (!VT)
472*0a6a1f1dSLionel Sambuc     return false;
473*0a6a1f1dSLionel Sambuc 
474*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
475*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(CI.getParent(), &CI);
476*0a6a1f1dSLionel Sambuc   Scatterer Op0 = scatter(&CI, CI.getOperand(0));
477*0a6a1f1dSLionel Sambuc   assert(Op0.size() == NumElems && "Mismatched cast");
478*0a6a1f1dSLionel Sambuc   ValueVector Res;
479*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
480*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I)
481*0a6a1f1dSLionel Sambuc     Res[I] = Builder.CreateCast(CI.getOpcode(), Op0[I], VT->getElementType(),
482*0a6a1f1dSLionel Sambuc                                 CI.getName() + ".i" + Twine(I));
483*0a6a1f1dSLionel Sambuc   gather(&CI, Res);
484*0a6a1f1dSLionel Sambuc   return true;
485*0a6a1f1dSLionel Sambuc }
486*0a6a1f1dSLionel Sambuc 
visitBitCastInst(BitCastInst & BCI)487*0a6a1f1dSLionel Sambuc bool Scalarizer::visitBitCastInst(BitCastInst &BCI) {
488*0a6a1f1dSLionel Sambuc   VectorType *DstVT = dyn_cast<VectorType>(BCI.getDestTy());
489*0a6a1f1dSLionel Sambuc   VectorType *SrcVT = dyn_cast<VectorType>(BCI.getSrcTy());
490*0a6a1f1dSLionel Sambuc   if (!DstVT || !SrcVT)
491*0a6a1f1dSLionel Sambuc     return false;
492*0a6a1f1dSLionel Sambuc 
493*0a6a1f1dSLionel Sambuc   unsigned DstNumElems = DstVT->getNumElements();
494*0a6a1f1dSLionel Sambuc   unsigned SrcNumElems = SrcVT->getNumElements();
495*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(BCI.getParent(), &BCI);
496*0a6a1f1dSLionel Sambuc   Scatterer Op0 = scatter(&BCI, BCI.getOperand(0));
497*0a6a1f1dSLionel Sambuc   ValueVector Res;
498*0a6a1f1dSLionel Sambuc   Res.resize(DstNumElems);
499*0a6a1f1dSLionel Sambuc 
500*0a6a1f1dSLionel Sambuc   if (DstNumElems == SrcNumElems) {
501*0a6a1f1dSLionel Sambuc     for (unsigned I = 0; I < DstNumElems; ++I)
502*0a6a1f1dSLionel Sambuc       Res[I] = Builder.CreateBitCast(Op0[I], DstVT->getElementType(),
503*0a6a1f1dSLionel Sambuc                                      BCI.getName() + ".i" + Twine(I));
504*0a6a1f1dSLionel Sambuc   } else if (DstNumElems > SrcNumElems) {
505*0a6a1f1dSLionel Sambuc     // <M x t1> -> <N*M x t2>.  Convert each t1 to <N x t2> and copy the
506*0a6a1f1dSLionel Sambuc     // individual elements to the destination.
507*0a6a1f1dSLionel Sambuc     unsigned FanOut = DstNumElems / SrcNumElems;
508*0a6a1f1dSLionel Sambuc     Type *MidTy = VectorType::get(DstVT->getElementType(), FanOut);
509*0a6a1f1dSLionel Sambuc     unsigned ResI = 0;
510*0a6a1f1dSLionel Sambuc     for (unsigned Op0I = 0; Op0I < SrcNumElems; ++Op0I) {
511*0a6a1f1dSLionel Sambuc       Value *V = Op0[Op0I];
512*0a6a1f1dSLionel Sambuc       Instruction *VI;
513*0a6a1f1dSLionel Sambuc       // Look through any existing bitcasts before converting to <N x t2>.
514*0a6a1f1dSLionel Sambuc       // In the best case, the resulting conversion might be a no-op.
515*0a6a1f1dSLionel Sambuc       while ((VI = dyn_cast<Instruction>(V)) &&
516*0a6a1f1dSLionel Sambuc              VI->getOpcode() == Instruction::BitCast)
517*0a6a1f1dSLionel Sambuc         V = VI->getOperand(0);
518*0a6a1f1dSLionel Sambuc       V = Builder.CreateBitCast(V, MidTy, V->getName() + ".cast");
519*0a6a1f1dSLionel Sambuc       Scatterer Mid = scatter(&BCI, V);
520*0a6a1f1dSLionel Sambuc       for (unsigned MidI = 0; MidI < FanOut; ++MidI)
521*0a6a1f1dSLionel Sambuc         Res[ResI++] = Mid[MidI];
522*0a6a1f1dSLionel Sambuc     }
523*0a6a1f1dSLionel Sambuc   } else {
524*0a6a1f1dSLionel Sambuc     // <N*M x t1> -> <M x t2>.  Convert each group of <N x t1> into a t2.
525*0a6a1f1dSLionel Sambuc     unsigned FanIn = SrcNumElems / DstNumElems;
526*0a6a1f1dSLionel Sambuc     Type *MidTy = VectorType::get(SrcVT->getElementType(), FanIn);
527*0a6a1f1dSLionel Sambuc     unsigned Op0I = 0;
528*0a6a1f1dSLionel Sambuc     for (unsigned ResI = 0; ResI < DstNumElems; ++ResI) {
529*0a6a1f1dSLionel Sambuc       Value *V = UndefValue::get(MidTy);
530*0a6a1f1dSLionel Sambuc       for (unsigned MidI = 0; MidI < FanIn; ++MidI)
531*0a6a1f1dSLionel Sambuc         V = Builder.CreateInsertElement(V, Op0[Op0I++], Builder.getInt32(MidI),
532*0a6a1f1dSLionel Sambuc                                         BCI.getName() + ".i" + Twine(ResI)
533*0a6a1f1dSLionel Sambuc                                         + ".upto" + Twine(MidI));
534*0a6a1f1dSLionel Sambuc       Res[ResI] = Builder.CreateBitCast(V, DstVT->getElementType(),
535*0a6a1f1dSLionel Sambuc                                         BCI.getName() + ".i" + Twine(ResI));
536*0a6a1f1dSLionel Sambuc     }
537*0a6a1f1dSLionel Sambuc   }
538*0a6a1f1dSLionel Sambuc   gather(&BCI, Res);
539*0a6a1f1dSLionel Sambuc   return true;
540*0a6a1f1dSLionel Sambuc }
541*0a6a1f1dSLionel Sambuc 
visitShuffleVectorInst(ShuffleVectorInst & SVI)542*0a6a1f1dSLionel Sambuc bool Scalarizer::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
543*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(SVI.getType());
544*0a6a1f1dSLionel Sambuc   if (!VT)
545*0a6a1f1dSLionel Sambuc     return false;
546*0a6a1f1dSLionel Sambuc 
547*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
548*0a6a1f1dSLionel Sambuc   Scatterer Op0 = scatter(&SVI, SVI.getOperand(0));
549*0a6a1f1dSLionel Sambuc   Scatterer Op1 = scatter(&SVI, SVI.getOperand(1));
550*0a6a1f1dSLionel Sambuc   ValueVector Res;
551*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
552*0a6a1f1dSLionel Sambuc 
553*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I) {
554*0a6a1f1dSLionel Sambuc     int Selector = SVI.getMaskValue(I);
555*0a6a1f1dSLionel Sambuc     if (Selector < 0)
556*0a6a1f1dSLionel Sambuc       Res[I] = UndefValue::get(VT->getElementType());
557*0a6a1f1dSLionel Sambuc     else if (unsigned(Selector) < Op0.size())
558*0a6a1f1dSLionel Sambuc       Res[I] = Op0[Selector];
559*0a6a1f1dSLionel Sambuc     else
560*0a6a1f1dSLionel Sambuc       Res[I] = Op1[Selector - Op0.size()];
561*0a6a1f1dSLionel Sambuc   }
562*0a6a1f1dSLionel Sambuc   gather(&SVI, Res);
563*0a6a1f1dSLionel Sambuc   return true;
564*0a6a1f1dSLionel Sambuc }
565*0a6a1f1dSLionel Sambuc 
visitPHINode(PHINode & PHI)566*0a6a1f1dSLionel Sambuc bool Scalarizer::visitPHINode(PHINode &PHI) {
567*0a6a1f1dSLionel Sambuc   VectorType *VT = dyn_cast<VectorType>(PHI.getType());
568*0a6a1f1dSLionel Sambuc   if (!VT)
569*0a6a1f1dSLionel Sambuc     return false;
570*0a6a1f1dSLionel Sambuc 
571*0a6a1f1dSLionel Sambuc   unsigned NumElems = VT->getNumElements();
572*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(PHI.getParent(), &PHI);
573*0a6a1f1dSLionel Sambuc   ValueVector Res;
574*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
575*0a6a1f1dSLionel Sambuc 
576*0a6a1f1dSLionel Sambuc   unsigned NumOps = PHI.getNumOperands();
577*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I)
578*0a6a1f1dSLionel Sambuc     Res[I] = Builder.CreatePHI(VT->getElementType(), NumOps,
579*0a6a1f1dSLionel Sambuc                                PHI.getName() + ".i" + Twine(I));
580*0a6a1f1dSLionel Sambuc 
581*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumOps; ++I) {
582*0a6a1f1dSLionel Sambuc     Scatterer Op = scatter(&PHI, PHI.getIncomingValue(I));
583*0a6a1f1dSLionel Sambuc     BasicBlock *IncomingBlock = PHI.getIncomingBlock(I);
584*0a6a1f1dSLionel Sambuc     for (unsigned J = 0; J < NumElems; ++J)
585*0a6a1f1dSLionel Sambuc       cast<PHINode>(Res[J])->addIncoming(Op[J], IncomingBlock);
586*0a6a1f1dSLionel Sambuc   }
587*0a6a1f1dSLionel Sambuc   gather(&PHI, Res);
588*0a6a1f1dSLionel Sambuc   return true;
589*0a6a1f1dSLionel Sambuc }
590*0a6a1f1dSLionel Sambuc 
visitLoadInst(LoadInst & LI)591*0a6a1f1dSLionel Sambuc bool Scalarizer::visitLoadInst(LoadInst &LI) {
592*0a6a1f1dSLionel Sambuc   if (!ScalarizeLoadStore)
593*0a6a1f1dSLionel Sambuc     return false;
594*0a6a1f1dSLionel Sambuc   if (!LI.isSimple())
595*0a6a1f1dSLionel Sambuc     return false;
596*0a6a1f1dSLionel Sambuc 
597*0a6a1f1dSLionel Sambuc   VectorLayout Layout;
598*0a6a1f1dSLionel Sambuc   if (!getVectorLayout(LI.getType(), LI.getAlignment(), Layout))
599*0a6a1f1dSLionel Sambuc     return false;
600*0a6a1f1dSLionel Sambuc 
601*0a6a1f1dSLionel Sambuc   unsigned NumElems = Layout.VecTy->getNumElements();
602*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(LI.getParent(), &LI);
603*0a6a1f1dSLionel Sambuc   Scatterer Ptr = scatter(&LI, LI.getPointerOperand());
604*0a6a1f1dSLionel Sambuc   ValueVector Res;
605*0a6a1f1dSLionel Sambuc   Res.resize(NumElems);
606*0a6a1f1dSLionel Sambuc 
607*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I)
608*0a6a1f1dSLionel Sambuc     Res[I] = Builder.CreateAlignedLoad(Ptr[I], Layout.getElemAlign(I),
609*0a6a1f1dSLionel Sambuc                                        LI.getName() + ".i" + Twine(I));
610*0a6a1f1dSLionel Sambuc   gather(&LI, Res);
611*0a6a1f1dSLionel Sambuc   return true;
612*0a6a1f1dSLionel Sambuc }
613*0a6a1f1dSLionel Sambuc 
visitStoreInst(StoreInst & SI)614*0a6a1f1dSLionel Sambuc bool Scalarizer::visitStoreInst(StoreInst &SI) {
615*0a6a1f1dSLionel Sambuc   if (!ScalarizeLoadStore)
616*0a6a1f1dSLionel Sambuc     return false;
617*0a6a1f1dSLionel Sambuc   if (!SI.isSimple())
618*0a6a1f1dSLionel Sambuc     return false;
619*0a6a1f1dSLionel Sambuc 
620*0a6a1f1dSLionel Sambuc   VectorLayout Layout;
621*0a6a1f1dSLionel Sambuc   Value *FullValue = SI.getValueOperand();
622*0a6a1f1dSLionel Sambuc   if (!getVectorLayout(FullValue->getType(), SI.getAlignment(), Layout))
623*0a6a1f1dSLionel Sambuc     return false;
624*0a6a1f1dSLionel Sambuc 
625*0a6a1f1dSLionel Sambuc   unsigned NumElems = Layout.VecTy->getNumElements();
626*0a6a1f1dSLionel Sambuc   IRBuilder<> Builder(SI.getParent(), &SI);
627*0a6a1f1dSLionel Sambuc   Scatterer Ptr = scatter(&SI, SI.getPointerOperand());
628*0a6a1f1dSLionel Sambuc   Scatterer Val = scatter(&SI, FullValue);
629*0a6a1f1dSLionel Sambuc 
630*0a6a1f1dSLionel Sambuc   ValueVector Stores;
631*0a6a1f1dSLionel Sambuc   Stores.resize(NumElems);
632*0a6a1f1dSLionel Sambuc   for (unsigned I = 0; I < NumElems; ++I) {
633*0a6a1f1dSLionel Sambuc     unsigned Align = Layout.getElemAlign(I);
634*0a6a1f1dSLionel Sambuc     Stores[I] = Builder.CreateAlignedStore(Val[I], Ptr[I], Align);
635*0a6a1f1dSLionel Sambuc   }
636*0a6a1f1dSLionel Sambuc   transferMetadata(&SI, Stores);
637*0a6a1f1dSLionel Sambuc   return true;
638*0a6a1f1dSLionel Sambuc }
639*0a6a1f1dSLionel Sambuc 
640*0a6a1f1dSLionel Sambuc // Delete the instructions that we scalarized.  If a full vector result
641*0a6a1f1dSLionel Sambuc // is still needed, recreate it using InsertElements.
finish()642*0a6a1f1dSLionel Sambuc bool Scalarizer::finish() {
643*0a6a1f1dSLionel Sambuc   if (Gathered.empty())
644*0a6a1f1dSLionel Sambuc     return false;
645*0a6a1f1dSLionel Sambuc   for (GatherList::iterator GMI = Gathered.begin(), GME = Gathered.end();
646*0a6a1f1dSLionel Sambuc        GMI != GME; ++GMI) {
647*0a6a1f1dSLionel Sambuc     Instruction *Op = GMI->first;
648*0a6a1f1dSLionel Sambuc     ValueVector &CV = *GMI->second;
649*0a6a1f1dSLionel Sambuc     if (!Op->use_empty()) {
650*0a6a1f1dSLionel Sambuc       // The value is still needed, so recreate it using a series of
651*0a6a1f1dSLionel Sambuc       // InsertElements.
652*0a6a1f1dSLionel Sambuc       Type *Ty = Op->getType();
653*0a6a1f1dSLionel Sambuc       Value *Res = UndefValue::get(Ty);
654*0a6a1f1dSLionel Sambuc       BasicBlock *BB = Op->getParent();
655*0a6a1f1dSLionel Sambuc       unsigned Count = Ty->getVectorNumElements();
656*0a6a1f1dSLionel Sambuc       IRBuilder<> Builder(BB, Op);
657*0a6a1f1dSLionel Sambuc       if (isa<PHINode>(Op))
658*0a6a1f1dSLionel Sambuc         Builder.SetInsertPoint(BB, BB->getFirstInsertionPt());
659*0a6a1f1dSLionel Sambuc       for (unsigned I = 0; I < Count; ++I)
660*0a6a1f1dSLionel Sambuc         Res = Builder.CreateInsertElement(Res, CV[I], Builder.getInt32(I),
661*0a6a1f1dSLionel Sambuc                                           Op->getName() + ".upto" + Twine(I));
662*0a6a1f1dSLionel Sambuc       Res->takeName(Op);
663*0a6a1f1dSLionel Sambuc       Op->replaceAllUsesWith(Res);
664*0a6a1f1dSLionel Sambuc     }
665*0a6a1f1dSLionel Sambuc     Op->eraseFromParent();
666*0a6a1f1dSLionel Sambuc   }
667*0a6a1f1dSLionel Sambuc   Gathered.clear();
668*0a6a1f1dSLionel Sambuc   Scattered.clear();
669*0a6a1f1dSLionel Sambuc   return true;
670*0a6a1f1dSLionel Sambuc }
671*0a6a1f1dSLionel Sambuc 
createScalarizerPass()672*0a6a1f1dSLionel Sambuc FunctionPass *llvm::createScalarizerPass() {
673*0a6a1f1dSLionel Sambuc   return new Scalarizer();
674*0a6a1f1dSLionel Sambuc }
675