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 //
10 // This pass implements the Bottom Up SLP vectorizer. It detects consecutive
11 // stores that can be put together into vector-stores. Next, it attempts to
12 // construct vectorizable tree using the use-def chains. If a profitable tree
13 // was found, the SLP vectorizer performs vectorization on the tree.
14 //
15 // The pass is inspired by the work described in the paper:
16 //  "Loop-Aware SLP in GCC" by Ira Rosen, Dorit Nuzman, Ayal Zaks.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/DenseSet.h"
24 #include "llvm/ADT/MapVector.h"
25 #include "llvm/ADT/None.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/PostOrderIterator.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/SmallSet.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/iterator.h"
35 #include "llvm/ADT/iterator_range.h"
36 #include "llvm/Analysis/AliasAnalysis.h"
37 #include "llvm/Analysis/CodeMetrics.h"
38 #include "llvm/Analysis/DemandedBits.h"
39 #include "llvm/Analysis/GlobalsModRef.h"
40 #include "llvm/Analysis/LoopAccessAnalysis.h"
41 #include "llvm/Analysis/LoopInfo.h"
42 #include "llvm/Analysis/MemoryLocation.h"
43 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
44 #include "llvm/Analysis/ScalarEvolution.h"
45 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
46 #include "llvm/Analysis/TargetLibraryInfo.h"
47 #include "llvm/Analysis/TargetTransformInfo.h"
48 #include "llvm/Analysis/ValueTracking.h"
49 #include "llvm/Analysis/VectorUtils.h"
50 #include "llvm/IR/Attributes.h"
51 #include "llvm/IR/BasicBlock.h"
52 #include "llvm/IR/Constant.h"
53 #include "llvm/IR/Constants.h"
54 #include "llvm/IR/DataLayout.h"
55 #include "llvm/IR/DebugLoc.h"
56 #include "llvm/IR/DerivedTypes.h"
57 #include "llvm/IR/Dominators.h"
58 #include "llvm/IR/Function.h"
59 #include "llvm/IR/IRBuilder.h"
60 #include "llvm/IR/InstrTypes.h"
61 #include "llvm/IR/Instruction.h"
62 #include "llvm/IR/Instructions.h"
63 #include "llvm/IR/IntrinsicInst.h"
64 #include "llvm/IR/Intrinsics.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/IR/NoFolder.h"
67 #include "llvm/IR/Operator.h"
68 #include "llvm/IR/PassManager.h"
69 #include "llvm/IR/PatternMatch.h"
70 #include "llvm/IR/Type.h"
71 #include "llvm/IR/Use.h"
72 #include "llvm/IR/User.h"
73 #include "llvm/IR/Value.h"
74 #include "llvm/IR/ValueHandle.h"
75 #include "llvm/IR/Verifier.h"
76 #include "llvm/Pass.h"
77 #include "llvm/Support/Casting.h"
78 #include "llvm/Support/CommandLine.h"
79 #include "llvm/Support/Compiler.h"
80 #include "llvm/Support/DOTGraphTraits.h"
81 #include "llvm/Support/Debug.h"
82 #include "llvm/Support/ErrorHandling.h"
83 #include "llvm/Support/GraphWriter.h"
84 #include "llvm/Support/KnownBits.h"
85 #include "llvm/Support/MathExtras.h"
86 #include "llvm/Support/raw_ostream.h"
87 #include "llvm/Transforms/Utils/LoopUtils.h"
88 #include "llvm/Transforms/Vectorize.h"
89 #include <algorithm>
90 #include <cassert>
91 #include <cstdint>
92 #include <iterator>
93 #include <memory>
94 #include <set>
95 #include <string>
96 #include <tuple>
97 #include <utility>
98 #include <vector>
99 
100 using namespace llvm;
101 using namespace llvm::PatternMatch;
102 using namespace slpvectorizer;
103 
104 #define SV_NAME "slp-vectorizer"
105 #define DEBUG_TYPE "SLP"
106 
107 STATISTIC(NumVectorInstructions, "Number of vector instructions generated");
108 
109 static cl::opt<int>
110     SLPCostThreshold("slp-threshold", cl::init(0), cl::Hidden,
111                      cl::desc("Only vectorize if you gain more than this "
112                               "number "));
113 
114 static cl::opt<bool>
115 ShouldVectorizeHor("slp-vectorize-hor", cl::init(true), cl::Hidden,
116                    cl::desc("Attempt to vectorize horizontal reductions"));
117 
118 static cl::opt<bool> ShouldStartVectorizeHorAtStore(
119     "slp-vectorize-hor-store", cl::init(false), cl::Hidden,
120     cl::desc(
121         "Attempt to vectorize horizontal reductions feeding into a store"));
122 
123 static cl::opt<int>
124 MaxVectorRegSizeOption("slp-max-reg-size", cl::init(128), cl::Hidden,
125     cl::desc("Attempt to vectorize for this register size in bits"));
126 
127 /// Limits the size of scheduling regions in a block.
128 /// It avoid long compile times for _very_ large blocks where vector
129 /// instructions are spread over a wide range.
130 /// This limit is way higher than needed by real-world functions.
131 static cl::opt<int>
132 ScheduleRegionSizeBudget("slp-schedule-budget", cl::init(100000), cl::Hidden,
133     cl::desc("Limit the size of the SLP scheduling region per block"));
134 
135 static cl::opt<int> MinVectorRegSizeOption(
136     "slp-min-reg-size", cl::init(128), cl::Hidden,
137     cl::desc("Attempt to vectorize for this register size in bits"));
138 
139 static cl::opt<unsigned> RecursionMaxDepth(
140     "slp-recursion-max-depth", cl::init(12), cl::Hidden,
141     cl::desc("Limit the recursion depth when building a vectorizable tree"));
142 
143 static cl::opt<unsigned> MinTreeSize(
144     "slp-min-tree-size", cl::init(3), cl::Hidden,
145     cl::desc("Only vectorize small trees if they are fully vectorizable"));
146 
147 static cl::opt<bool>
148     ViewSLPTree("view-slp-tree", cl::Hidden,
149                 cl::desc("Display the SLP trees with Graphviz"));
150 
151 // Limit the number of alias checks. The limit is chosen so that
152 // it has no negative effect on the llvm benchmarks.
153 static const unsigned AliasedCheckLimit = 10;
154 
155 // Another limit for the alias checks: The maximum distance between load/store
156 // instructions where alias checks are done.
157 // This limit is useful for very large basic blocks.
158 static const unsigned MaxMemDepDistance = 160;
159 
160 /// If the ScheduleRegionSizeBudget is exhausted, we allow small scheduling
161 /// regions to be handled.
162 static const int MinScheduleRegionSize = 16;
163 
164 /// \brief Predicate for the element types that the SLP vectorizer supports.
165 ///
166 /// The most important thing to filter here are types which are invalid in LLVM
167 /// vectors. We also filter target specific types which have absolutely no
168 /// meaningful vectorization path such as x86_fp80 and ppc_f128. This just
169 /// avoids spending time checking the cost model and realizing that they will
170 /// be inevitably scalarized.
171 static bool isValidElementType(Type *Ty) {
172   return VectorType::isValidElementType(Ty) && !Ty->isX86_FP80Ty() &&
173          !Ty->isPPC_FP128Ty();
174 }
175 
176 /// \returns true if all of the instructions in \p VL are in the same block or
177 /// false otherwise.
178 static bool allSameBlock(ArrayRef<Value *> VL) {
179   Instruction *I0 = dyn_cast<Instruction>(VL[0]);
180   if (!I0)
181     return false;
182   BasicBlock *BB = I0->getParent();
183   for (int i = 1, e = VL.size(); i < e; i++) {
184     Instruction *I = dyn_cast<Instruction>(VL[i]);
185     if (!I)
186       return false;
187 
188     if (BB != I->getParent())
189       return false;
190   }
191   return true;
192 }
193 
194 /// \returns True if all of the values in \p VL are constants.
195 static bool allConstant(ArrayRef<Value *> VL) {
196   for (Value *i : VL)
197     if (!isa<Constant>(i))
198       return false;
199   return true;
200 }
201 
202 /// \returns True if all of the values in \p VL are identical.
203 static bool isSplat(ArrayRef<Value *> VL) {
204   for (unsigned i = 1, e = VL.size(); i < e; ++i)
205     if (VL[i] != VL[0])
206       return false;
207   return true;
208 }
209 
210 /// Checks if the vector of instructions can be represented as a shuffle, like:
211 /// %x0 = extractelement <4 x i8> %x, i32 0
212 /// %x3 = extractelement <4 x i8> %x, i32 3
213 /// %y1 = extractelement <4 x i8> %y, i32 1
214 /// %y2 = extractelement <4 x i8> %y, i32 2
215 /// %x0x0 = mul i8 %x0, %x0
216 /// %x3x3 = mul i8 %x3, %x3
217 /// %y1y1 = mul i8 %y1, %y1
218 /// %y2y2 = mul i8 %y2, %y2
219 /// %ins1 = insertelement <4 x i8> undef, i8 %x0x0, i32 0
220 /// %ins2 = insertelement <4 x i8> %ins1, i8 %x3x3, i32 1
221 /// %ins3 = insertelement <4 x i8> %ins2, i8 %y1y1, i32 2
222 /// %ins4 = insertelement <4 x i8> %ins3, i8 %y2y2, i32 3
223 /// ret <4 x i8> %ins4
224 /// can be transformed into:
225 /// %1 = shufflevector <4 x i8> %x, <4 x i8> %y, <4 x i32> <i32 0, i32 3, i32 5,
226 ///                                                         i32 6>
227 /// %2 = mul <4 x i8> %1, %1
228 /// ret <4 x i8> %2
229 /// We convert this initially to something like:
230 /// %x0 = extractelement <4 x i8> %x, i32 0
231 /// %x3 = extractelement <4 x i8> %x, i32 3
232 /// %y1 = extractelement <4 x i8> %y, i32 1
233 /// %y2 = extractelement <4 x i8> %y, i32 2
234 /// %1 = insertelement <4 x i8> undef, i8 %x0, i32 0
235 /// %2 = insertelement <4 x i8> %1, i8 %x3, i32 1
236 /// %3 = insertelement <4 x i8> %2, i8 %y1, i32 2
237 /// %4 = insertelement <4 x i8> %3, i8 %y2, i32 3
238 /// %5 = mul <4 x i8> %4, %4
239 /// %6 = extractelement <4 x i8> %5, i32 0
240 /// %ins1 = insertelement <4 x i8> undef, i8 %6, i32 0
241 /// %7 = extractelement <4 x i8> %5, i32 1
242 /// %ins2 = insertelement <4 x i8> %ins1, i8 %7, i32 1
243 /// %8 = extractelement <4 x i8> %5, i32 2
244 /// %ins3 = insertelement <4 x i8> %ins2, i8 %8, i32 2
245 /// %9 = extractelement <4 x i8> %5, i32 3
246 /// %ins4 = insertelement <4 x i8> %ins3, i8 %9, i32 3
247 /// ret <4 x i8> %ins4
248 /// InstCombiner transforms this into a shuffle and vector mul
249 static Optional<TargetTransformInfo::ShuffleKind>
250 isShuffle(ArrayRef<Value *> VL) {
251   auto *EI0 = cast<ExtractElementInst>(VL[0]);
252   unsigned Size = EI0->getVectorOperandType()->getVectorNumElements();
253   Value *Vec1 = nullptr;
254   Value *Vec2 = nullptr;
255   enum ShuffleMode {Unknown, FirstAlternate, SecondAlternate, Permute};
256   ShuffleMode CommonShuffleMode = Unknown;
257   for (unsigned I = 0, E = VL.size(); I < E; ++I) {
258     auto *EI = cast<ExtractElementInst>(VL[I]);
259     auto *Vec = EI->getVectorOperand();
260     // All vector operands must have the same number of vector elements.
261     if (Vec->getType()->getVectorNumElements() != Size)
262       return None;
263     auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
264     if (!Idx)
265       return None;
266     // Undefined behavior if Idx is negative or >= Size.
267     if (Idx->getValue().uge(Size))
268       continue;
269     unsigned IntIdx = Idx->getValue().getZExtValue();
270     // We can extractelement from undef vector.
271     if (isa<UndefValue>(Vec))
272       continue;
273     // For correct shuffling we have to have at most 2 different vector operands
274     // in all extractelement instructions.
275     if (Vec1 && Vec2 && Vec != Vec1 && Vec != Vec2)
276       return None;
277     if (CommonShuffleMode == Permute)
278       continue;
279     // If the extract index is not the same as the operation number, it is a
280     // permutation.
281     if (IntIdx != I) {
282       CommonShuffleMode = Permute;
283       continue;
284     }
285     // Check the shuffle mode for the current operation.
286     if (!Vec1)
287       Vec1 = Vec;
288     else if (Vec != Vec1)
289       Vec2 = Vec;
290     // Example: shufflevector A, B, <0,5,2,7>
291     // I is odd and IntIdx for A == I - FirstAlternate shuffle.
292     // I is even and IntIdx for B == I - FirstAlternate shuffle.
293     // Example: shufflevector A, B, <4,1,6,3>
294     // I is even and IntIdx for A == I - SecondAlternate shuffle.
295     // I is odd and IntIdx for B == I - SecondAlternate shuffle.
296     const bool IIsEven = I & 1;
297     const bool CurrVecIsA = Vec == Vec1;
298     const bool IIsOdd = !IIsEven;
299     const bool CurrVecIsB = !CurrVecIsA;
300     ShuffleMode CurrentShuffleMode =
301         ((IIsOdd && CurrVecIsA) || (IIsEven && CurrVecIsB)) ? FirstAlternate
302                                                             : SecondAlternate;
303     // Common mode is not set or the same as the shuffle mode of the current
304     // operation - alternate.
305     if (CommonShuffleMode == Unknown)
306       CommonShuffleMode = CurrentShuffleMode;
307     // Common shuffle mode is not the same as the shuffle mode of the current
308     // operation - permutation.
309     if (CommonShuffleMode != CurrentShuffleMode)
310       CommonShuffleMode = Permute;
311   }
312   // If we're not crossing lanes in different vectors, consider it as blending.
313   if ((CommonShuffleMode == FirstAlternate ||
314        CommonShuffleMode == SecondAlternate) &&
315       Vec2)
316     return TargetTransformInfo::SK_Alternate;
317   // If Vec2 was never used, we have a permutation of a single vector, otherwise
318   // we have permutation of 2 vectors.
319   return Vec2 ? TargetTransformInfo::SK_PermuteTwoSrc
320               : TargetTransformInfo::SK_PermuteSingleSrc;
321 }
322 
323 ///\returns Opcode that can be clubbed with \p Op to create an alternate
324 /// sequence which can later be merged as a ShuffleVector instruction.
325 static unsigned getAltOpcode(unsigned Op) {
326   switch (Op) {
327   case Instruction::FAdd:
328     return Instruction::FSub;
329   case Instruction::FSub:
330     return Instruction::FAdd;
331   case Instruction::Add:
332     return Instruction::Sub;
333   case Instruction::Sub:
334     return Instruction::Add;
335   default:
336     return 0;
337   }
338 }
339 
340 static bool isOdd(unsigned Value) {
341   return Value & 1;
342 }
343 
344 static bool sameOpcodeOrAlt(unsigned Opcode, unsigned AltOpcode,
345                             unsigned CheckedOpcode) {
346   return Opcode == CheckedOpcode || AltOpcode == CheckedOpcode;
347 }
348 
349 /// Chooses the correct key for scheduling data. If \p Op has the same (or
350 /// alternate) opcode as \p OpValue, the key is \p Op. Otherwise the key is \p
351 /// OpValue.
352 static Value *isOneOf(Value *OpValue, Value *Op) {
353   auto *I = dyn_cast<Instruction>(Op);
354   if (!I)
355     return OpValue;
356   auto *OpInst = cast<Instruction>(OpValue);
357   unsigned OpInstOpcode = OpInst->getOpcode();
358   unsigned IOpcode = I->getOpcode();
359   if (sameOpcodeOrAlt(OpInstOpcode, getAltOpcode(OpInstOpcode), IOpcode))
360     return Op;
361   return OpValue;
362 }
363 
364 namespace {
365 
366 /// Contains data for the instructions going to be vectorized.
367 struct RawInstructionsData {
368   /// Main Opcode of the instructions going to be vectorized.
369   unsigned Opcode = 0;
370 
371   /// The list of instructions have some instructions with alternate opcodes.
372   bool HasAltOpcodes = false;
373 };
374 
375 } // end anonymous namespace
376 
377 /// Checks the list of the vectorized instructions \p VL and returns info about
378 /// this list.
379 static RawInstructionsData getMainOpcode(ArrayRef<Value *> VL) {
380   auto *I0 = dyn_cast<Instruction>(VL[0]);
381   if (!I0)
382     return {};
383   RawInstructionsData Res;
384   unsigned Opcode = I0->getOpcode();
385   // Walk through the list of the vectorized instructions
386   // in order to check its structure described by RawInstructionsData.
387   for (unsigned Cnt = 0, E = VL.size(); Cnt != E; ++Cnt) {
388     auto *I = dyn_cast<Instruction>(VL[Cnt]);
389     if (!I)
390       return {};
391     if (Opcode != I->getOpcode())
392       Res.HasAltOpcodes = true;
393   }
394   Res.Opcode = Opcode;
395   return Res;
396 }
397 
398 namespace {
399 
400 /// Main data required for vectorization of instructions.
401 struct InstructionsState {
402   /// The very first instruction in the list with the main opcode.
403   Value *OpValue = nullptr;
404 
405   /// The main opcode for the list of instructions.
406   unsigned Opcode = 0;
407 
408   /// Some of the instructions in the list have alternate opcodes.
409   bool IsAltShuffle = false;
410 
411   InstructionsState() = default;
412   InstructionsState(Value *OpValue, unsigned Opcode, bool IsAltShuffle)
413       : OpValue(OpValue), Opcode(Opcode), IsAltShuffle(IsAltShuffle) {}
414 };
415 
416 } // end anonymous namespace
417 
418 /// \returns analysis of the Instructions in \p VL described in
419 /// InstructionsState, the Opcode that we suppose the whole list
420 /// could be vectorized even if its structure is diverse.
421 static InstructionsState getSameOpcode(ArrayRef<Value *> VL) {
422   auto Res = getMainOpcode(VL);
423   unsigned Opcode = Res.Opcode;
424   if (!Res.HasAltOpcodes)
425     return InstructionsState(VL[0], Opcode, false);
426   auto *OpInst = cast<Instruction>(VL[0]);
427   unsigned AltOpcode = getAltOpcode(Opcode);
428   // Examine each element in the list instructions VL to determine
429   // if some operations there could be considered as an alternative
430   // (for example as subtraction relates to addition operation).
431   for (int Cnt = 0, E = VL.size(); Cnt < E; Cnt++) {
432     auto *I = cast<Instruction>(VL[Cnt]);
433     unsigned InstOpcode = I->getOpcode();
434     if ((Res.HasAltOpcodes &&
435          InstOpcode != (isOdd(Cnt) ? AltOpcode : Opcode)) ||
436         (!Res.HasAltOpcodes && InstOpcode != Opcode)) {
437       return InstructionsState(OpInst, 0, false);
438     }
439   }
440   return InstructionsState(OpInst, Opcode, Res.HasAltOpcodes);
441 }
442 
443 /// \returns true if all of the values in \p VL have the same type or false
444 /// otherwise.
445 static bool allSameType(ArrayRef<Value *> VL) {
446   Type *Ty = VL[0]->getType();
447   for (int i = 1, e = VL.size(); i < e; i++)
448     if (VL[i]->getType() != Ty)
449       return false;
450 
451   return true;
452 }
453 
454 /// \returns True if Extract{Value,Element} instruction extracts element Idx.
455 static bool matchExtractIndex(Instruction *E, unsigned Idx, unsigned Opcode) {
456   assert(Opcode == Instruction::ExtractElement ||
457          Opcode == Instruction::ExtractValue);
458   if (Opcode == Instruction::ExtractElement) {
459     ConstantInt *CI = dyn_cast<ConstantInt>(E->getOperand(1));
460     return CI && CI->getZExtValue() == Idx;
461   } else {
462     ExtractValueInst *EI = cast<ExtractValueInst>(E);
463     return EI->getNumIndices() == 1 && *EI->idx_begin() == Idx;
464   }
465 }
466 
467 /// \returns True if in-tree use also needs extract. This refers to
468 /// possible scalar operand in vectorized instruction.
469 static bool InTreeUserNeedToExtract(Value *Scalar, Instruction *UserInst,
470                                     TargetLibraryInfo *TLI) {
471   unsigned Opcode = UserInst->getOpcode();
472   switch (Opcode) {
473   case Instruction::Load: {
474     LoadInst *LI = cast<LoadInst>(UserInst);
475     return (LI->getPointerOperand() == Scalar);
476   }
477   case Instruction::Store: {
478     StoreInst *SI = cast<StoreInst>(UserInst);
479     return (SI->getPointerOperand() == Scalar);
480   }
481   case Instruction::Call: {
482     CallInst *CI = cast<CallInst>(UserInst);
483     Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
484     if (hasVectorInstrinsicScalarOpd(ID, 1)) {
485       return (CI->getArgOperand(1) == Scalar);
486     }
487     LLVM_FALLTHROUGH;
488   }
489   default:
490     return false;
491   }
492 }
493 
494 /// \returns the AA location that is being access by the instruction.
495 static MemoryLocation getLocation(Instruction *I, AliasAnalysis *AA) {
496   if (StoreInst *SI = dyn_cast<StoreInst>(I))
497     return MemoryLocation::get(SI);
498   if (LoadInst *LI = dyn_cast<LoadInst>(I))
499     return MemoryLocation::get(LI);
500   return MemoryLocation();
501 }
502 
503 /// \returns True if the instruction is not a volatile or atomic load/store.
504 static bool isSimple(Instruction *I) {
505   if (LoadInst *LI = dyn_cast<LoadInst>(I))
506     return LI->isSimple();
507   if (StoreInst *SI = dyn_cast<StoreInst>(I))
508     return SI->isSimple();
509   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
510     return !MI->isVolatile();
511   return true;
512 }
513 
514 namespace llvm {
515 
516 namespace slpvectorizer {
517 
518 /// Bottom Up SLP Vectorizer.
519 class BoUpSLP {
520 public:
521   using ValueList = SmallVector<Value *, 8>;
522   using InstrList = SmallVector<Instruction *, 16>;
523   using ValueSet = SmallPtrSet<Value *, 16>;
524   using StoreList = SmallVector<StoreInst *, 8>;
525   using ExtraValueToDebugLocsMap =
526       MapVector<Value *, SmallVector<Instruction *, 2>>;
527 
528   BoUpSLP(Function *Func, ScalarEvolution *Se, TargetTransformInfo *Tti,
529           TargetLibraryInfo *TLi, AliasAnalysis *Aa, LoopInfo *Li,
530           DominatorTree *Dt, AssumptionCache *AC, DemandedBits *DB,
531           const DataLayout *DL, OptimizationRemarkEmitter *ORE)
532       : F(Func), SE(Se), TTI(Tti), TLI(TLi), AA(Aa), LI(Li), DT(Dt), AC(AC),
533         DB(DB), DL(DL), ORE(ORE), Builder(Se->getContext()) {
534     CodeMetrics::collectEphemeralValues(F, AC, EphValues);
535     // Use the vector register size specified by the target unless overridden
536     // by a command-line option.
537     // TODO: It would be better to limit the vectorization factor based on
538     //       data type rather than just register size. For example, x86 AVX has
539     //       256-bit registers, but it does not support integer operations
540     //       at that width (that requires AVX2).
541     if (MaxVectorRegSizeOption.getNumOccurrences())
542       MaxVecRegSize = MaxVectorRegSizeOption;
543     else
544       MaxVecRegSize = TTI->getRegisterBitWidth(true);
545 
546     if (MinVectorRegSizeOption.getNumOccurrences())
547       MinVecRegSize = MinVectorRegSizeOption;
548     else
549       MinVecRegSize = TTI->getMinVectorRegisterBitWidth();
550   }
551 
552   /// \brief Vectorize the tree that starts with the elements in \p VL.
553   /// Returns the vectorized root.
554   Value *vectorizeTree();
555 
556   /// Vectorize the tree but with the list of externally used values \p
557   /// ExternallyUsedValues. Values in this MapVector can be replaced but the
558   /// generated extractvalue instructions.
559   Value *vectorizeTree(ExtraValueToDebugLocsMap &ExternallyUsedValues);
560 
561   /// \returns the cost incurred by unwanted spills and fills, caused by
562   /// holding live values over call sites.
563   int getSpillCost();
564 
565   /// \returns the vectorization cost of the subtree that starts at \p VL.
566   /// A negative number means that this is profitable.
567   int getTreeCost();
568 
569   /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
570   /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
571   void buildTree(ArrayRef<Value *> Roots,
572                  ArrayRef<Value *> UserIgnoreLst = None);
573 
574   /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
575   /// the purpose of scheduling and extraction in the \p UserIgnoreLst taking
576   /// into account (anf updating it, if required) list of externally used
577   /// values stored in \p ExternallyUsedValues.
578   void buildTree(ArrayRef<Value *> Roots,
579                  ExtraValueToDebugLocsMap &ExternallyUsedValues,
580                  ArrayRef<Value *> UserIgnoreLst = None);
581 
582   /// Clear the internal data structures that are created by 'buildTree'.
583   void deleteTree() {
584     VectorizableTree.clear();
585     ScalarToTreeEntry.clear();
586     MustGather.clear();
587     ExternalUses.clear();
588     NumLoadsWantToKeepOrder = 0;
589     NumLoadsWantToChangeOrder = 0;
590     for (auto &Iter : BlocksSchedules) {
591       BlockScheduling *BS = Iter.second.get();
592       BS->clear();
593     }
594     MinBWs.clear();
595   }
596 
597   unsigned getTreeSize() const { return VectorizableTree.size(); }
598 
599   /// \brief Perform LICM and CSE on the newly generated gather sequences.
600   void optimizeGatherSequence(Function &F);
601 
602   /// \returns true if it is beneficial to reverse the vector order.
603   bool shouldReorder() const {
604     return NumLoadsWantToChangeOrder > NumLoadsWantToKeepOrder;
605   }
606 
607   /// \return The vector element size in bits to use when vectorizing the
608   /// expression tree ending at \p V. If V is a store, the size is the width of
609   /// the stored value. Otherwise, the size is the width of the largest loaded
610   /// value reaching V. This method is used by the vectorizer to calculate
611   /// vectorization factors.
612   unsigned getVectorElementSize(Value *V);
613 
614   /// Compute the minimum type sizes required to represent the entries in a
615   /// vectorizable tree.
616   void computeMinimumValueSizes();
617 
618   // \returns maximum vector register size as set by TTI or overridden by cl::opt.
619   unsigned getMaxVecRegSize() const {
620     return MaxVecRegSize;
621   }
622 
623   // \returns minimum vector register size as set by cl::opt.
624   unsigned getMinVecRegSize() const {
625     return MinVecRegSize;
626   }
627 
628   /// \brief Check if ArrayType or StructType is isomorphic to some VectorType.
629   ///
630   /// \returns number of elements in vector if isomorphism exists, 0 otherwise.
631   unsigned canMapToVector(Type *T, const DataLayout &DL) const;
632 
633   /// \returns True if the VectorizableTree is both tiny and not fully
634   /// vectorizable. We do not vectorize such trees.
635   bool isTreeTinyAndNotFullyVectorizable();
636 
637   OptimizationRemarkEmitter *getORE() { return ORE; }
638 
639 private:
640   struct TreeEntry;
641 
642   /// Checks if all users of \p I are the part of the vectorization tree.
643   bool areAllUsersVectorized(Instruction *I) const;
644 
645   /// \returns the cost of the vectorizable entry.
646   int getEntryCost(TreeEntry *E);
647 
648   /// This is the recursive part of buildTree.
649   void buildTree_rec(ArrayRef<Value *> Roots, unsigned Depth, int);
650 
651   /// \returns True if the ExtractElement/ExtractValue instructions in VL can
652   /// be vectorized to use the original vector (or aggregate "bitcast" to a vector).
653   bool canReuseExtract(ArrayRef<Value *> VL, Value *OpValue) const;
654 
655   /// Vectorize a single entry in the tree.
656   Value *vectorizeTree(TreeEntry *E);
657 
658   /// Vectorize a single entry in the tree, starting in \p VL.
659   Value *vectorizeTree(ArrayRef<Value *> VL);
660 
661   /// \returns the pointer to the vectorized value if \p VL is already
662   /// vectorized, or NULL. They may happen in cycles.
663   Value *alreadyVectorized(ArrayRef<Value *> VL, Value *OpValue) const;
664 
665   /// \returns the scalarization cost for this type. Scalarization in this
666   /// context means the creation of vectors from a group of scalars.
667   int getGatherCost(Type *Ty);
668 
669   /// \returns the scalarization cost for this list of values. Assuming that
670   /// this subtree gets vectorized, we may need to extract the values from the
671   /// roots. This method calculates the cost of extracting the values.
672   int getGatherCost(ArrayRef<Value *> VL);
673 
674   /// \brief Set the Builder insert point to one after the last instruction in
675   /// the bundle
676   void setInsertPointAfterBundle(ArrayRef<Value *> VL, Value *OpValue);
677 
678   /// \returns a vector from a collection of scalars in \p VL.
679   Value *Gather(ArrayRef<Value *> VL, VectorType *Ty);
680 
681   /// \returns whether the VectorizableTree is fully vectorizable and will
682   /// be beneficial even the tree height is tiny.
683   bool isFullyVectorizableTinyTree();
684 
685   /// \reorder commutative operands in alt shuffle if they result in
686   ///  vectorized code.
687   void reorderAltShuffleOperands(unsigned Opcode, ArrayRef<Value *> VL,
688                                  SmallVectorImpl<Value *> &Left,
689                                  SmallVectorImpl<Value *> &Right);
690 
691   /// \reorder commutative operands to get better probability of
692   /// generating vectorized code.
693   void reorderInputsAccordingToOpcode(unsigned Opcode, ArrayRef<Value *> VL,
694                                       SmallVectorImpl<Value *> &Left,
695                                       SmallVectorImpl<Value *> &Right);
696   struct TreeEntry {
697     TreeEntry(std::vector<TreeEntry> &Container) : Container(Container) {}
698 
699     /// \returns true if the scalars in VL are equal to this entry.
700     bool isSame(ArrayRef<Value *> VL) const {
701       assert(VL.size() == Scalars.size() && "Invalid size");
702       return std::equal(VL.begin(), VL.end(), Scalars.begin());
703     }
704 
705     /// A vector of scalars.
706     ValueList Scalars;
707 
708     /// The Scalars are vectorized into this value. It is initialized to Null.
709     Value *VectorizedValue = nullptr;
710 
711     /// Do we need to gather this sequence ?
712     bool NeedToGather = false;
713 
714     /// Points back to the VectorizableTree.
715     ///
716     /// Only used for Graphviz right now.  Unfortunately GraphTrait::NodeRef has
717     /// to be a pointer and needs to be able to initialize the child iterator.
718     /// Thus we need a reference back to the container to translate the indices
719     /// to entries.
720     std::vector<TreeEntry> &Container;
721 
722     /// The TreeEntry index containing the user of this entry.  We can actually
723     /// have multiple users so the data structure is not truly a tree.
724     SmallVector<int, 1> UserTreeIndices;
725   };
726 
727   /// Create a new VectorizableTree entry.
728   TreeEntry *newTreeEntry(ArrayRef<Value *> VL, bool Vectorized,
729                           int &UserTreeIdx) {
730     VectorizableTree.emplace_back(VectorizableTree);
731     int idx = VectorizableTree.size() - 1;
732     TreeEntry *Last = &VectorizableTree[idx];
733     Last->Scalars.insert(Last->Scalars.begin(), VL.begin(), VL.end());
734     Last->NeedToGather = !Vectorized;
735     if (Vectorized) {
736       for (int i = 0, e = VL.size(); i != e; ++i) {
737         assert(!getTreeEntry(VL[i]) && "Scalar already in tree!");
738         ScalarToTreeEntry[VL[i]] = idx;
739       }
740     } else {
741       MustGather.insert(VL.begin(), VL.end());
742     }
743 
744     if (UserTreeIdx >= 0)
745       Last->UserTreeIndices.push_back(UserTreeIdx);
746     UserTreeIdx = idx;
747     return Last;
748   }
749 
750   /// -- Vectorization State --
751   /// Holds all of the tree entries.
752   std::vector<TreeEntry> VectorizableTree;
753 
754   TreeEntry *getTreeEntry(Value *V) {
755     auto I = ScalarToTreeEntry.find(V);
756     if (I != ScalarToTreeEntry.end())
757       return &VectorizableTree[I->second];
758     return nullptr;
759   }
760 
761   const TreeEntry *getTreeEntry(Value *V) const {
762     auto I = ScalarToTreeEntry.find(V);
763     if (I != ScalarToTreeEntry.end())
764       return &VectorizableTree[I->second];
765     return nullptr;
766   }
767 
768   /// Maps a specific scalar to its tree entry.
769   SmallDenseMap<Value*, int> ScalarToTreeEntry;
770 
771   /// A list of scalars that we found that we need to keep as scalars.
772   ValueSet MustGather;
773 
774   /// This POD struct describes one external user in the vectorized tree.
775   struct ExternalUser {
776     ExternalUser(Value *S, llvm::User *U, int L)
777         : Scalar(S), User(U), Lane(L) {}
778 
779     // Which scalar in our function.
780     Value *Scalar;
781 
782     // Which user that uses the scalar.
783     llvm::User *User;
784 
785     // Which lane does the scalar belong to.
786     int Lane;
787   };
788   using UserList = SmallVector<ExternalUser, 16>;
789 
790   /// Checks if two instructions may access the same memory.
791   ///
792   /// \p Loc1 is the location of \p Inst1. It is passed explicitly because it
793   /// is invariant in the calling loop.
794   bool isAliased(const MemoryLocation &Loc1, Instruction *Inst1,
795                  Instruction *Inst2) {
796     // First check if the result is already in the cache.
797     AliasCacheKey key = std::make_pair(Inst1, Inst2);
798     Optional<bool> &result = AliasCache[key];
799     if (result.hasValue()) {
800       return result.getValue();
801     }
802     MemoryLocation Loc2 = getLocation(Inst2, AA);
803     bool aliased = true;
804     if (Loc1.Ptr && Loc2.Ptr && isSimple(Inst1) && isSimple(Inst2)) {
805       // Do the alias check.
806       aliased = AA->alias(Loc1, Loc2);
807     }
808     // Store the result in the cache.
809     result = aliased;
810     return aliased;
811   }
812 
813   using AliasCacheKey = std::pair<Instruction *, Instruction *>;
814 
815   /// Cache for alias results.
816   /// TODO: consider moving this to the AliasAnalysis itself.
817   DenseMap<AliasCacheKey, Optional<bool>> AliasCache;
818 
819   /// Removes an instruction from its block and eventually deletes it.
820   /// It's like Instruction::eraseFromParent() except that the actual deletion
821   /// is delayed until BoUpSLP is destructed.
822   /// This is required to ensure that there are no incorrect collisions in the
823   /// AliasCache, which can happen if a new instruction is allocated at the
824   /// same address as a previously deleted instruction.
825   void eraseInstruction(Instruction *I) {
826     I->removeFromParent();
827     I->dropAllReferences();
828     DeletedInstructions.emplace_back(I);
829   }
830 
831   /// Temporary store for deleted instructions. Instructions will be deleted
832   /// eventually when the BoUpSLP is destructed.
833   SmallVector<unique_value, 8> DeletedInstructions;
834 
835   /// A list of values that need to extracted out of the tree.
836   /// This list holds pairs of (Internal Scalar : External User). External User
837   /// can be nullptr, it means that this Internal Scalar will be used later,
838   /// after vectorization.
839   UserList ExternalUses;
840 
841   /// Values used only by @llvm.assume calls.
842   SmallPtrSet<const Value *, 32> EphValues;
843 
844   /// Holds all of the instructions that we gathered.
845   SetVector<Instruction *> GatherSeq;
846 
847   /// A list of blocks that we are going to CSE.
848   SetVector<BasicBlock *> CSEBlocks;
849 
850   /// Contains all scheduling relevant data for an instruction.
851   /// A ScheduleData either represents a single instruction or a member of an
852   /// instruction bundle (= a group of instructions which is combined into a
853   /// vector instruction).
854   struct ScheduleData {
855     // The initial value for the dependency counters. It means that the
856     // dependencies are not calculated yet.
857     enum { InvalidDeps = -1 };
858 
859     ScheduleData() = default;
860 
861     void init(int BlockSchedulingRegionID, Value *OpVal) {
862       FirstInBundle = this;
863       NextInBundle = nullptr;
864       NextLoadStore = nullptr;
865       IsScheduled = false;
866       SchedulingRegionID = BlockSchedulingRegionID;
867       UnscheduledDepsInBundle = UnscheduledDeps;
868       clearDependencies();
869       OpValue = OpVal;
870     }
871 
872     /// Returns true if the dependency information has been calculated.
873     bool hasValidDependencies() const { return Dependencies != InvalidDeps; }
874 
875     /// Returns true for single instructions and for bundle representatives
876     /// (= the head of a bundle).
877     bool isSchedulingEntity() const { return FirstInBundle == this; }
878 
879     /// Returns true if it represents an instruction bundle and not only a
880     /// single instruction.
881     bool isPartOfBundle() const {
882       return NextInBundle != nullptr || FirstInBundle != this;
883     }
884 
885     /// Returns true if it is ready for scheduling, i.e. it has no more
886     /// unscheduled depending instructions/bundles.
887     bool isReady() const {
888       assert(isSchedulingEntity() &&
889              "can't consider non-scheduling entity for ready list");
890       return UnscheduledDepsInBundle == 0 && !IsScheduled;
891     }
892 
893     /// Modifies the number of unscheduled dependencies, also updating it for
894     /// the whole bundle.
895     int incrementUnscheduledDeps(int Incr) {
896       UnscheduledDeps += Incr;
897       return FirstInBundle->UnscheduledDepsInBundle += Incr;
898     }
899 
900     /// Sets the number of unscheduled dependencies to the number of
901     /// dependencies.
902     void resetUnscheduledDeps() {
903       incrementUnscheduledDeps(Dependencies - UnscheduledDeps);
904     }
905 
906     /// Clears all dependency information.
907     void clearDependencies() {
908       Dependencies = InvalidDeps;
909       resetUnscheduledDeps();
910       MemoryDependencies.clear();
911     }
912 
913     void dump(raw_ostream &os) const {
914       if (!isSchedulingEntity()) {
915         os << "/ " << *Inst;
916       } else if (NextInBundle) {
917         os << '[' << *Inst;
918         ScheduleData *SD = NextInBundle;
919         while (SD) {
920           os << ';' << *SD->Inst;
921           SD = SD->NextInBundle;
922         }
923         os << ']';
924       } else {
925         os << *Inst;
926       }
927     }
928 
929     Instruction *Inst = nullptr;
930 
931     /// Points to the head in an instruction bundle (and always to this for
932     /// single instructions).
933     ScheduleData *FirstInBundle = nullptr;
934 
935     /// Single linked list of all instructions in a bundle. Null if it is a
936     /// single instruction.
937     ScheduleData *NextInBundle = nullptr;
938 
939     /// Single linked list of all memory instructions (e.g. load, store, call)
940     /// in the block - until the end of the scheduling region.
941     ScheduleData *NextLoadStore = nullptr;
942 
943     /// The dependent memory instructions.
944     /// This list is derived on demand in calculateDependencies().
945     SmallVector<ScheduleData *, 4> MemoryDependencies;
946 
947     /// This ScheduleData is in the current scheduling region if this matches
948     /// the current SchedulingRegionID of BlockScheduling.
949     int SchedulingRegionID = 0;
950 
951     /// Used for getting a "good" final ordering of instructions.
952     int SchedulingPriority = 0;
953 
954     /// The number of dependencies. Constitutes of the number of users of the
955     /// instruction plus the number of dependent memory instructions (if any).
956     /// This value is calculated on demand.
957     /// If InvalidDeps, the number of dependencies is not calculated yet.
958     int Dependencies = InvalidDeps;
959 
960     /// The number of dependencies minus the number of dependencies of scheduled
961     /// instructions. As soon as this is zero, the instruction/bundle gets ready
962     /// for scheduling.
963     /// Note that this is negative as long as Dependencies is not calculated.
964     int UnscheduledDeps = InvalidDeps;
965 
966     /// The sum of UnscheduledDeps in a bundle. Equals to UnscheduledDeps for
967     /// single instructions.
968     int UnscheduledDepsInBundle = InvalidDeps;
969 
970     /// True if this instruction is scheduled (or considered as scheduled in the
971     /// dry-run).
972     bool IsScheduled = false;
973 
974     /// Opcode of the current instruction in the schedule data.
975     Value *OpValue = nullptr;
976   };
977 
978 #ifndef NDEBUG
979   friend inline raw_ostream &operator<<(raw_ostream &os,
980                                         const BoUpSLP::ScheduleData &SD) {
981     SD.dump(os);
982     return os;
983   }
984 #endif
985 
986   friend struct GraphTraits<BoUpSLP *>;
987   friend struct DOTGraphTraits<BoUpSLP *>;
988 
989   /// Contains all scheduling data for a basic block.
990   struct BlockScheduling {
991     BlockScheduling(BasicBlock *BB)
992         : BB(BB), ChunkSize(BB->size()), ChunkPos(ChunkSize) {}
993 
994     void clear() {
995       ReadyInsts.clear();
996       ScheduleStart = nullptr;
997       ScheduleEnd = nullptr;
998       FirstLoadStoreInRegion = nullptr;
999       LastLoadStoreInRegion = nullptr;
1000 
1001       // Reduce the maximum schedule region size by the size of the
1002       // previous scheduling run.
1003       ScheduleRegionSizeLimit -= ScheduleRegionSize;
1004       if (ScheduleRegionSizeLimit < MinScheduleRegionSize)
1005         ScheduleRegionSizeLimit = MinScheduleRegionSize;
1006       ScheduleRegionSize = 0;
1007 
1008       // Make a new scheduling region, i.e. all existing ScheduleData is not
1009       // in the new region yet.
1010       ++SchedulingRegionID;
1011     }
1012 
1013     ScheduleData *getScheduleData(Value *V) {
1014       ScheduleData *SD = ScheduleDataMap[V];
1015       if (SD && SD->SchedulingRegionID == SchedulingRegionID)
1016         return SD;
1017       return nullptr;
1018     }
1019 
1020     ScheduleData *getScheduleData(Value *V, Value *Key) {
1021       if (V == Key)
1022         return getScheduleData(V);
1023       auto I = ExtraScheduleDataMap.find(V);
1024       if (I != ExtraScheduleDataMap.end()) {
1025         ScheduleData *SD = I->second[Key];
1026         if (SD && SD->SchedulingRegionID == SchedulingRegionID)
1027           return SD;
1028       }
1029       return nullptr;
1030     }
1031 
1032     bool isInSchedulingRegion(ScheduleData *SD) {
1033       return SD->SchedulingRegionID == SchedulingRegionID;
1034     }
1035 
1036     /// Marks an instruction as scheduled and puts all dependent ready
1037     /// instructions into the ready-list.
1038     template <typename ReadyListType>
1039     void schedule(ScheduleData *SD, ReadyListType &ReadyList) {
1040       SD->IsScheduled = true;
1041       DEBUG(dbgs() << "SLP:   schedule " << *SD << "\n");
1042 
1043       ScheduleData *BundleMember = SD;
1044       while (BundleMember) {
1045         if (BundleMember->Inst != BundleMember->OpValue) {
1046           BundleMember = BundleMember->NextInBundle;
1047           continue;
1048         }
1049         // Handle the def-use chain dependencies.
1050         for (Use &U : BundleMember->Inst->operands()) {
1051           auto *I = dyn_cast<Instruction>(U.get());
1052           if (!I)
1053             continue;
1054           doForAllOpcodes(I, [&ReadyList](ScheduleData *OpDef) {
1055             if (OpDef && OpDef->hasValidDependencies() &&
1056                 OpDef->incrementUnscheduledDeps(-1) == 0) {
1057               // There are no more unscheduled dependencies after
1058               // decrementing, so we can put the dependent instruction
1059               // into the ready list.
1060               ScheduleData *DepBundle = OpDef->FirstInBundle;
1061               assert(!DepBundle->IsScheduled &&
1062                      "already scheduled bundle gets ready");
1063               ReadyList.insert(DepBundle);
1064               DEBUG(dbgs()
1065                     << "SLP:    gets ready (def): " << *DepBundle << "\n");
1066             }
1067           });
1068         }
1069         // Handle the memory dependencies.
1070         for (ScheduleData *MemoryDepSD : BundleMember->MemoryDependencies) {
1071           if (MemoryDepSD->incrementUnscheduledDeps(-1) == 0) {
1072             // There are no more unscheduled dependencies after decrementing,
1073             // so we can put the dependent instruction into the ready list.
1074             ScheduleData *DepBundle = MemoryDepSD->FirstInBundle;
1075             assert(!DepBundle->IsScheduled &&
1076                    "already scheduled bundle gets ready");
1077             ReadyList.insert(DepBundle);
1078             DEBUG(dbgs() << "SLP:    gets ready (mem): " << *DepBundle
1079                          << "\n");
1080           }
1081         }
1082         BundleMember = BundleMember->NextInBundle;
1083       }
1084     }
1085 
1086     void doForAllOpcodes(Value *V,
1087                          function_ref<void(ScheduleData *SD)> Action) {
1088       if (ScheduleData *SD = getScheduleData(V))
1089         Action(SD);
1090       auto I = ExtraScheduleDataMap.find(V);
1091       if (I != ExtraScheduleDataMap.end())
1092         for (auto &P : I->second)
1093           if (P.second->SchedulingRegionID == SchedulingRegionID)
1094             Action(P.second);
1095     }
1096 
1097     /// Put all instructions into the ReadyList which are ready for scheduling.
1098     template <typename ReadyListType>
1099     void initialFillReadyList(ReadyListType &ReadyList) {
1100       for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
1101         doForAllOpcodes(I, [&](ScheduleData *SD) {
1102           if (SD->isSchedulingEntity() && SD->isReady()) {
1103             ReadyList.insert(SD);
1104             DEBUG(dbgs() << "SLP:    initially in ready list: " << *I << "\n");
1105           }
1106         });
1107       }
1108     }
1109 
1110     /// Checks if a bundle of instructions can be scheduled, i.e. has no
1111     /// cyclic dependencies. This is only a dry-run, no instructions are
1112     /// actually moved at this stage.
1113     bool tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP, Value *OpValue);
1114 
1115     /// Un-bundles a group of instructions.
1116     void cancelScheduling(ArrayRef<Value *> VL, Value *OpValue);
1117 
1118     /// Allocates schedule data chunk.
1119     ScheduleData *allocateScheduleDataChunks();
1120 
1121     /// Extends the scheduling region so that V is inside the region.
1122     /// \returns true if the region size is within the limit.
1123     bool extendSchedulingRegion(Value *V, Value *OpValue);
1124 
1125     /// Initialize the ScheduleData structures for new instructions in the
1126     /// scheduling region.
1127     void initScheduleData(Instruction *FromI, Instruction *ToI,
1128                           ScheduleData *PrevLoadStore,
1129                           ScheduleData *NextLoadStore);
1130 
1131     /// Updates the dependency information of a bundle and of all instructions/
1132     /// bundles which depend on the original bundle.
1133     void calculateDependencies(ScheduleData *SD, bool InsertInReadyList,
1134                                BoUpSLP *SLP);
1135 
1136     /// Sets all instruction in the scheduling region to un-scheduled.
1137     void resetSchedule();
1138 
1139     BasicBlock *BB;
1140 
1141     /// Simple memory allocation for ScheduleData.
1142     std::vector<std::unique_ptr<ScheduleData[]>> ScheduleDataChunks;
1143 
1144     /// The size of a ScheduleData array in ScheduleDataChunks.
1145     int ChunkSize;
1146 
1147     /// The allocator position in the current chunk, which is the last entry
1148     /// of ScheduleDataChunks.
1149     int ChunkPos;
1150 
1151     /// Attaches ScheduleData to Instruction.
1152     /// Note that the mapping survives during all vectorization iterations, i.e.
1153     /// ScheduleData structures are recycled.
1154     DenseMap<Value *, ScheduleData *> ScheduleDataMap;
1155 
1156     /// Attaches ScheduleData to Instruction with the leading key.
1157     DenseMap<Value *, SmallDenseMap<Value *, ScheduleData *>>
1158         ExtraScheduleDataMap;
1159 
1160     struct ReadyList : SmallVector<ScheduleData *, 8> {
1161       void insert(ScheduleData *SD) { push_back(SD); }
1162     };
1163 
1164     /// The ready-list for scheduling (only used for the dry-run).
1165     ReadyList ReadyInsts;
1166 
1167     /// The first instruction of the scheduling region.
1168     Instruction *ScheduleStart = nullptr;
1169 
1170     /// The first instruction _after_ the scheduling region.
1171     Instruction *ScheduleEnd = nullptr;
1172 
1173     /// The first memory accessing instruction in the scheduling region
1174     /// (can be null).
1175     ScheduleData *FirstLoadStoreInRegion = nullptr;
1176 
1177     /// The last memory accessing instruction in the scheduling region
1178     /// (can be null).
1179     ScheduleData *LastLoadStoreInRegion = nullptr;
1180 
1181     /// The current size of the scheduling region.
1182     int ScheduleRegionSize = 0;
1183 
1184     /// The maximum size allowed for the scheduling region.
1185     int ScheduleRegionSizeLimit = ScheduleRegionSizeBudget;
1186 
1187     /// The ID of the scheduling region. For a new vectorization iteration this
1188     /// is incremented which "removes" all ScheduleData from the region.
1189     // Make sure that the initial SchedulingRegionID is greater than the
1190     // initial SchedulingRegionID in ScheduleData (which is 0).
1191     int SchedulingRegionID = 1;
1192   };
1193 
1194   /// Attaches the BlockScheduling structures to basic blocks.
1195   MapVector<BasicBlock *, std::unique_ptr<BlockScheduling>> BlocksSchedules;
1196 
1197   /// Performs the "real" scheduling. Done before vectorization is actually
1198   /// performed in a basic block.
1199   void scheduleBlock(BlockScheduling *BS);
1200 
1201   /// List of users to ignore during scheduling and that don't need extracting.
1202   ArrayRef<Value *> UserIgnoreList;
1203 
1204   // Number of load bundles that contain consecutive loads.
1205   int NumLoadsWantToKeepOrder = 0;
1206 
1207   // Number of load bundles that contain consecutive loads in reversed order.
1208   int NumLoadsWantToChangeOrder = 0;
1209 
1210   // Analysis and block reference.
1211   Function *F;
1212   ScalarEvolution *SE;
1213   TargetTransformInfo *TTI;
1214   TargetLibraryInfo *TLI;
1215   AliasAnalysis *AA;
1216   LoopInfo *LI;
1217   DominatorTree *DT;
1218   AssumptionCache *AC;
1219   DemandedBits *DB;
1220   const DataLayout *DL;
1221   OptimizationRemarkEmitter *ORE;
1222 
1223   unsigned MaxVecRegSize; // This is set by TTI or overridden by cl::opt.
1224   unsigned MinVecRegSize; // Set by cl::opt (default: 128).
1225 
1226   /// Instruction builder to construct the vectorized tree.
1227   IRBuilder<> Builder;
1228 
1229   /// A map of scalar integer values to the smallest bit width with which they
1230   /// can legally be represented. The values map to (width, signed) pairs,
1231   /// where "width" indicates the minimum bit width and "signed" is True if the
1232   /// value must be signed-extended, rather than zero-extended, back to its
1233   /// original width.
1234   MapVector<Value *, std::pair<uint64_t, bool>> MinBWs;
1235 };
1236 
1237 } // end namespace slpvectorizer
1238 
1239 template <> struct GraphTraits<BoUpSLP *> {
1240   using TreeEntry = BoUpSLP::TreeEntry;
1241 
1242   /// NodeRef has to be a pointer per the GraphWriter.
1243   using NodeRef = TreeEntry *;
1244 
1245   /// \brief Add the VectorizableTree to the index iterator to be able to return
1246   /// TreeEntry pointers.
1247   struct ChildIteratorType
1248       : public iterator_adaptor_base<ChildIteratorType,
1249                                      SmallVector<int, 1>::iterator> {
1250     std::vector<TreeEntry> &VectorizableTree;
1251 
1252     ChildIteratorType(SmallVector<int, 1>::iterator W,
1253                       std::vector<TreeEntry> &VT)
1254         : ChildIteratorType::iterator_adaptor_base(W), VectorizableTree(VT) {}
1255 
1256     NodeRef operator*() { return &VectorizableTree[*I]; }
1257   };
1258 
1259   static NodeRef getEntryNode(BoUpSLP &R) { return &R.VectorizableTree[0]; }
1260 
1261   static ChildIteratorType child_begin(NodeRef N) {
1262     return {N->UserTreeIndices.begin(), N->Container};
1263   }
1264 
1265   static ChildIteratorType child_end(NodeRef N) {
1266     return {N->UserTreeIndices.end(), N->Container};
1267   }
1268 
1269   /// For the node iterator we just need to turn the TreeEntry iterator into a
1270   /// TreeEntry* iterator so that it dereferences to NodeRef.
1271   using nodes_iterator = pointer_iterator<std::vector<TreeEntry>::iterator>;
1272 
1273   static nodes_iterator nodes_begin(BoUpSLP *R) {
1274     return nodes_iterator(R->VectorizableTree.begin());
1275   }
1276 
1277   static nodes_iterator nodes_end(BoUpSLP *R) {
1278     return nodes_iterator(R->VectorizableTree.end());
1279   }
1280 
1281   static unsigned size(BoUpSLP *R) { return R->VectorizableTree.size(); }
1282 };
1283 
1284 template <> struct DOTGraphTraits<BoUpSLP *> : public DefaultDOTGraphTraits {
1285   using TreeEntry = BoUpSLP::TreeEntry;
1286 
1287   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
1288 
1289   std::string getNodeLabel(const TreeEntry *Entry, const BoUpSLP *R) {
1290     std::string Str;
1291     raw_string_ostream OS(Str);
1292     if (isSplat(Entry->Scalars)) {
1293       OS << "<splat> " << *Entry->Scalars[0];
1294       return Str;
1295     }
1296     for (auto V : Entry->Scalars) {
1297       OS << *V;
1298       if (std::any_of(
1299               R->ExternalUses.begin(), R->ExternalUses.end(),
1300               [&](const BoUpSLP::ExternalUser &EU) { return EU.Scalar == V; }))
1301         OS << " <extract>";
1302       OS << "\n";
1303     }
1304     return Str;
1305   }
1306 
1307   static std::string getNodeAttributes(const TreeEntry *Entry,
1308                                        const BoUpSLP *) {
1309     if (Entry->NeedToGather)
1310       return "color=red";
1311     return "";
1312   }
1313 };
1314 
1315 } // end namespace llvm
1316 
1317 void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
1318                         ArrayRef<Value *> UserIgnoreLst) {
1319   ExtraValueToDebugLocsMap ExternallyUsedValues;
1320   buildTree(Roots, ExternallyUsedValues, UserIgnoreLst);
1321 }
1322 
1323 void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
1324                         ExtraValueToDebugLocsMap &ExternallyUsedValues,
1325                         ArrayRef<Value *> UserIgnoreLst) {
1326   deleteTree();
1327   UserIgnoreList = UserIgnoreLst;
1328   if (!allSameType(Roots))
1329     return;
1330   buildTree_rec(Roots, 0, -1);
1331 
1332   // Collect the values that we need to extract from the tree.
1333   for (TreeEntry &EIdx : VectorizableTree) {
1334     TreeEntry *Entry = &EIdx;
1335 
1336     // No need to handle users of gathered values.
1337     if (Entry->NeedToGather)
1338       continue;
1339 
1340     // For each lane:
1341     for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
1342       Value *Scalar = Entry->Scalars[Lane];
1343 
1344       // Check if the scalar is externally used as an extra arg.
1345       auto ExtI = ExternallyUsedValues.find(Scalar);
1346       if (ExtI != ExternallyUsedValues.end()) {
1347         DEBUG(dbgs() << "SLP: Need to extract: Extra arg from lane " <<
1348               Lane << " from " << *Scalar << ".\n");
1349         ExternalUses.emplace_back(Scalar, nullptr, Lane);
1350       }
1351       for (User *U : Scalar->users()) {
1352         DEBUG(dbgs() << "SLP: Checking user:" << *U << ".\n");
1353 
1354         Instruction *UserInst = dyn_cast<Instruction>(U);
1355         if (!UserInst)
1356           continue;
1357 
1358         // Skip in-tree scalars that become vectors
1359         if (TreeEntry *UseEntry = getTreeEntry(U)) {
1360           Value *UseScalar = UseEntry->Scalars[0];
1361           // Some in-tree scalars will remain as scalar in vectorized
1362           // instructions. If that is the case, the one in Lane 0 will
1363           // be used.
1364           if (UseScalar != U ||
1365               !InTreeUserNeedToExtract(Scalar, UserInst, TLI)) {
1366             DEBUG(dbgs() << "SLP: \tInternal user will be removed:" << *U
1367                          << ".\n");
1368             assert(!UseEntry->NeedToGather && "Bad state");
1369             continue;
1370           }
1371         }
1372 
1373         // Ignore users in the user ignore list.
1374         if (is_contained(UserIgnoreList, UserInst))
1375           continue;
1376 
1377         DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " <<
1378               Lane << " from " << *Scalar << ".\n");
1379         ExternalUses.push_back(ExternalUser(Scalar, U, Lane));
1380       }
1381     }
1382   }
1383 }
1384 
1385 void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
1386                             int UserTreeIdx) {
1387   assert((allConstant(VL) || allSameType(VL)) && "Invalid types!");
1388 
1389   InstructionsState S = getSameOpcode(VL);
1390   if (Depth == RecursionMaxDepth) {
1391     DEBUG(dbgs() << "SLP: Gathering due to max recursion depth.\n");
1392     newTreeEntry(VL, false, UserTreeIdx);
1393     return;
1394   }
1395 
1396   // Don't handle vectors.
1397   if (S.OpValue->getType()->isVectorTy()) {
1398     DEBUG(dbgs() << "SLP: Gathering due to vector type.\n");
1399     newTreeEntry(VL, false, UserTreeIdx);
1400     return;
1401   }
1402 
1403   if (StoreInst *SI = dyn_cast<StoreInst>(S.OpValue))
1404     if (SI->getValueOperand()->getType()->isVectorTy()) {
1405       DEBUG(dbgs() << "SLP: Gathering due to store vector type.\n");
1406       newTreeEntry(VL, false, UserTreeIdx);
1407       return;
1408     }
1409 
1410   // If all of the operands are identical or constant we have a simple solution.
1411   if (allConstant(VL) || isSplat(VL) || !allSameBlock(VL) || !S.Opcode) {
1412     DEBUG(dbgs() << "SLP: Gathering due to C,S,B,O. \n");
1413     newTreeEntry(VL, false, UserTreeIdx);
1414     return;
1415   }
1416 
1417   // We now know that this is a vector of instructions of the same type from
1418   // the same block.
1419 
1420   // Don't vectorize ephemeral values.
1421   for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1422     if (EphValues.count(VL[i])) {
1423       DEBUG(dbgs() << "SLP: The instruction (" << *VL[i] <<
1424             ") is ephemeral.\n");
1425       newTreeEntry(VL, false, UserTreeIdx);
1426       return;
1427     }
1428   }
1429 
1430   // Check if this is a duplicate of another entry.
1431   if (TreeEntry *E = getTreeEntry(S.OpValue)) {
1432     for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1433       DEBUG(dbgs() << "SLP: \tChecking bundle: " << *VL[i] << ".\n");
1434       if (E->Scalars[i] != VL[i]) {
1435         DEBUG(dbgs() << "SLP: Gathering due to partial overlap.\n");
1436         newTreeEntry(VL, false, UserTreeIdx);
1437         return;
1438       }
1439     }
1440     // Record the reuse of the tree node.  FIXME, currently this is only used to
1441     // properly draw the graph rather than for the actual vectorization.
1442     E->UserTreeIndices.push_back(UserTreeIdx);
1443     DEBUG(dbgs() << "SLP: Perfect diamond merge at " << *S.OpValue << ".\n");
1444     return;
1445   }
1446 
1447   // Check that none of the instructions in the bundle are already in the tree.
1448   for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1449     auto *I = dyn_cast<Instruction>(VL[i]);
1450     if (!I)
1451       continue;
1452     if (getTreeEntry(I)) {
1453       DEBUG(dbgs() << "SLP: The instruction (" << *VL[i] <<
1454             ") is already in tree.\n");
1455       newTreeEntry(VL, false, UserTreeIdx);
1456       return;
1457     }
1458   }
1459 
1460   // If any of the scalars is marked as a value that needs to stay scalar, then
1461   // we need to gather the scalars.
1462   for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1463     if (MustGather.count(VL[i])) {
1464       DEBUG(dbgs() << "SLP: Gathering due to gathered scalar.\n");
1465       newTreeEntry(VL, false, UserTreeIdx);
1466       return;
1467     }
1468   }
1469 
1470   // Check that all of the users of the scalars that we want to vectorize are
1471   // schedulable.
1472   auto *VL0 = cast<Instruction>(S.OpValue);
1473   BasicBlock *BB = VL0->getParent();
1474 
1475   if (!DT->isReachableFromEntry(BB)) {
1476     // Don't go into unreachable blocks. They may contain instructions with
1477     // dependency cycles which confuse the final scheduling.
1478     DEBUG(dbgs() << "SLP: bundle in unreachable block.\n");
1479     newTreeEntry(VL, false, UserTreeIdx);
1480     return;
1481   }
1482 
1483   // Check that every instruction appears once in this bundle.
1484   for (unsigned i = 0, e = VL.size(); i < e; ++i)
1485     for (unsigned j = i + 1; j < e; ++j)
1486       if (VL[i] == VL[j]) {
1487         DEBUG(dbgs() << "SLP: Scalar used twice in bundle.\n");
1488         newTreeEntry(VL, false, UserTreeIdx);
1489         return;
1490       }
1491 
1492   auto &BSRef = BlocksSchedules[BB];
1493   if (!BSRef)
1494     BSRef = llvm::make_unique<BlockScheduling>(BB);
1495 
1496   BlockScheduling &BS = *BSRef.get();
1497 
1498   if (!BS.tryScheduleBundle(VL, this, S.OpValue)) {
1499     DEBUG(dbgs() << "SLP: We are not able to schedule this bundle!\n");
1500     assert((!BS.getScheduleData(VL0) ||
1501             !BS.getScheduleData(VL0)->isPartOfBundle()) &&
1502            "tryScheduleBundle should cancelScheduling on failure");
1503     newTreeEntry(VL, false, UserTreeIdx);
1504     return;
1505   }
1506   DEBUG(dbgs() << "SLP: We are able to schedule this bundle.\n");
1507 
1508   unsigned ShuffleOrOp = S.IsAltShuffle ?
1509                 (unsigned) Instruction::ShuffleVector : S.Opcode;
1510   switch (ShuffleOrOp) {
1511     case Instruction::PHI: {
1512       PHINode *PH = dyn_cast<PHINode>(VL0);
1513 
1514       // Check for terminator values (e.g. invoke).
1515       for (unsigned j = 0; j < VL.size(); ++j)
1516         for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1517           TerminatorInst *Term = dyn_cast<TerminatorInst>(
1518               cast<PHINode>(VL[j])->getIncomingValueForBlock(PH->getIncomingBlock(i)));
1519           if (Term) {
1520             DEBUG(dbgs() << "SLP: Need to swizzle PHINodes (TerminatorInst use).\n");
1521             BS.cancelScheduling(VL, VL0);
1522             newTreeEntry(VL, false, UserTreeIdx);
1523             return;
1524           }
1525         }
1526 
1527       newTreeEntry(VL, true, UserTreeIdx);
1528       DEBUG(dbgs() << "SLP: added a vector of PHINodes.\n");
1529 
1530       for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1531         ValueList Operands;
1532         // Prepare the operand vector.
1533         for (Value *j : VL)
1534           Operands.push_back(cast<PHINode>(j)->getIncomingValueForBlock(
1535               PH->getIncomingBlock(i)));
1536 
1537         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1538       }
1539       return;
1540     }
1541     case Instruction::ExtractValue:
1542     case Instruction::ExtractElement: {
1543       bool Reuse = canReuseExtract(VL, VL0);
1544       if (Reuse) {
1545         DEBUG(dbgs() << "SLP: Reusing extract sequence.\n");
1546       } else {
1547         BS.cancelScheduling(VL, VL0);
1548       }
1549       newTreeEntry(VL, Reuse, UserTreeIdx);
1550       return;
1551     }
1552     case Instruction::Load: {
1553       // Check that a vectorized load would load the same memory as a scalar
1554       // load. For example, we don't want to vectorize loads that are smaller
1555       // than 8-bit. Even though we have a packed struct {<i2, i2, i2, i2>} LLVM
1556       // treats loading/storing it as an i8 struct. If we vectorize loads/stores
1557       // from such a struct, we read/write packed bits disagreeing with the
1558       // unvectorized version.
1559       Type *ScalarTy = VL0->getType();
1560 
1561       if (DL->getTypeSizeInBits(ScalarTy) !=
1562           DL->getTypeAllocSizeInBits(ScalarTy)) {
1563         BS.cancelScheduling(VL, VL0);
1564         newTreeEntry(VL, false, UserTreeIdx);
1565         DEBUG(dbgs() << "SLP: Gathering loads of non-packed type.\n");
1566         return;
1567       }
1568 
1569       // Make sure all loads in the bundle are simple - we can't vectorize
1570       // atomic or volatile loads.
1571       for (unsigned i = 0, e = VL.size() - 1; i < e; ++i) {
1572         LoadInst *L = cast<LoadInst>(VL[i]);
1573         if (!L->isSimple()) {
1574           BS.cancelScheduling(VL, VL0);
1575           newTreeEntry(VL, false, UserTreeIdx);
1576           DEBUG(dbgs() << "SLP: Gathering non-simple loads.\n");
1577           return;
1578         }
1579       }
1580 
1581       // Check if the loads are consecutive, reversed, or neither.
1582       // TODO: What we really want is to sort the loads, but for now, check
1583       // the two likely directions.
1584       bool Consecutive = true;
1585       bool ReverseConsecutive = true;
1586       for (unsigned i = 0, e = VL.size() - 1; i < e; ++i) {
1587         if (!isConsecutiveAccess(VL[i], VL[i + 1], *DL, *SE)) {
1588           Consecutive = false;
1589           break;
1590         } else {
1591           ReverseConsecutive = false;
1592         }
1593       }
1594 
1595       if (Consecutive) {
1596         ++NumLoadsWantToKeepOrder;
1597         newTreeEntry(VL, true, UserTreeIdx);
1598         DEBUG(dbgs() << "SLP: added a vector of loads.\n");
1599         return;
1600       }
1601 
1602       // If none of the load pairs were consecutive when checked in order,
1603       // check the reverse order.
1604       if (ReverseConsecutive)
1605         for (unsigned i = VL.size() - 1; i > 0; --i)
1606           if (!isConsecutiveAccess(VL[i], VL[i - 1], *DL, *SE)) {
1607             ReverseConsecutive = false;
1608             break;
1609           }
1610 
1611       BS.cancelScheduling(VL, VL0);
1612       newTreeEntry(VL, false, UserTreeIdx);
1613 
1614       if (ReverseConsecutive) {
1615         ++NumLoadsWantToChangeOrder;
1616         DEBUG(dbgs() << "SLP: Gathering reversed loads.\n");
1617       } else {
1618         DEBUG(dbgs() << "SLP: Gathering non-consecutive loads.\n");
1619       }
1620       return;
1621     }
1622     case Instruction::ZExt:
1623     case Instruction::SExt:
1624     case Instruction::FPToUI:
1625     case Instruction::FPToSI:
1626     case Instruction::FPExt:
1627     case Instruction::PtrToInt:
1628     case Instruction::IntToPtr:
1629     case Instruction::SIToFP:
1630     case Instruction::UIToFP:
1631     case Instruction::Trunc:
1632     case Instruction::FPTrunc:
1633     case Instruction::BitCast: {
1634       Type *SrcTy = VL0->getOperand(0)->getType();
1635       for (unsigned i = 0; i < VL.size(); ++i) {
1636         Type *Ty = cast<Instruction>(VL[i])->getOperand(0)->getType();
1637         if (Ty != SrcTy || !isValidElementType(Ty)) {
1638           BS.cancelScheduling(VL, VL0);
1639           newTreeEntry(VL, false, UserTreeIdx);
1640           DEBUG(dbgs() << "SLP: Gathering casts with different src types.\n");
1641           return;
1642         }
1643       }
1644       newTreeEntry(VL, true, UserTreeIdx);
1645       DEBUG(dbgs() << "SLP: added a vector of casts.\n");
1646 
1647       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1648         ValueList Operands;
1649         // Prepare the operand vector.
1650         for (Value *j : VL)
1651           Operands.push_back(cast<Instruction>(j)->getOperand(i));
1652 
1653         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1654       }
1655       return;
1656     }
1657     case Instruction::ICmp:
1658     case Instruction::FCmp: {
1659       // Check that all of the compares have the same predicate.
1660       CmpInst::Predicate P0 = cast<CmpInst>(VL0)->getPredicate();
1661       Type *ComparedTy = VL0->getOperand(0)->getType();
1662       for (unsigned i = 1, e = VL.size(); i < e; ++i) {
1663         CmpInst *Cmp = cast<CmpInst>(VL[i]);
1664         if (Cmp->getPredicate() != P0 ||
1665             Cmp->getOperand(0)->getType() != ComparedTy) {
1666           BS.cancelScheduling(VL, VL0);
1667           newTreeEntry(VL, false, UserTreeIdx);
1668           DEBUG(dbgs() << "SLP: Gathering cmp with different predicate.\n");
1669           return;
1670         }
1671       }
1672 
1673       newTreeEntry(VL, true, UserTreeIdx);
1674       DEBUG(dbgs() << "SLP: added a vector of compares.\n");
1675 
1676       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1677         ValueList Operands;
1678         // Prepare the operand vector.
1679         for (Value *j : VL)
1680           Operands.push_back(cast<Instruction>(j)->getOperand(i));
1681 
1682         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1683       }
1684       return;
1685     }
1686     case Instruction::Select:
1687     case Instruction::Add:
1688     case Instruction::FAdd:
1689     case Instruction::Sub:
1690     case Instruction::FSub:
1691     case Instruction::Mul:
1692     case Instruction::FMul:
1693     case Instruction::UDiv:
1694     case Instruction::SDiv:
1695     case Instruction::FDiv:
1696     case Instruction::URem:
1697     case Instruction::SRem:
1698     case Instruction::FRem:
1699     case Instruction::Shl:
1700     case Instruction::LShr:
1701     case Instruction::AShr:
1702     case Instruction::And:
1703     case Instruction::Or:
1704     case Instruction::Xor:
1705       newTreeEntry(VL, true, UserTreeIdx);
1706       DEBUG(dbgs() << "SLP: added a vector of bin op.\n");
1707 
1708       // Sort operands of the instructions so that each side is more likely to
1709       // have the same opcode.
1710       if (isa<BinaryOperator>(VL0) && VL0->isCommutative()) {
1711         ValueList Left, Right;
1712         reorderInputsAccordingToOpcode(S.Opcode, VL, Left, Right);
1713         buildTree_rec(Left, Depth + 1, UserTreeIdx);
1714         buildTree_rec(Right, Depth + 1, UserTreeIdx);
1715         return;
1716       }
1717 
1718       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1719         ValueList Operands;
1720         // Prepare the operand vector.
1721         for (Value *j : VL)
1722           Operands.push_back(cast<Instruction>(j)->getOperand(i));
1723 
1724         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1725       }
1726       return;
1727 
1728     case Instruction::GetElementPtr: {
1729       // We don't combine GEPs with complicated (nested) indexing.
1730       for (unsigned j = 0; j < VL.size(); ++j) {
1731         if (cast<Instruction>(VL[j])->getNumOperands() != 2) {
1732           DEBUG(dbgs() << "SLP: not-vectorizable GEP (nested indexes).\n");
1733           BS.cancelScheduling(VL, VL0);
1734           newTreeEntry(VL, false, UserTreeIdx);
1735           return;
1736         }
1737       }
1738 
1739       // We can't combine several GEPs into one vector if they operate on
1740       // different types.
1741       Type *Ty0 = VL0->getOperand(0)->getType();
1742       for (unsigned j = 0; j < VL.size(); ++j) {
1743         Type *CurTy = cast<Instruction>(VL[j])->getOperand(0)->getType();
1744         if (Ty0 != CurTy) {
1745           DEBUG(dbgs() << "SLP: not-vectorizable GEP (different types).\n");
1746           BS.cancelScheduling(VL, VL0);
1747           newTreeEntry(VL, false, UserTreeIdx);
1748           return;
1749         }
1750       }
1751 
1752       // We don't combine GEPs with non-constant indexes.
1753       for (unsigned j = 0; j < VL.size(); ++j) {
1754         auto Op = cast<Instruction>(VL[j])->getOperand(1);
1755         if (!isa<ConstantInt>(Op)) {
1756           DEBUG(
1757               dbgs() << "SLP: not-vectorizable GEP (non-constant indexes).\n");
1758           BS.cancelScheduling(VL, VL0);
1759           newTreeEntry(VL, false, UserTreeIdx);
1760           return;
1761         }
1762       }
1763 
1764       newTreeEntry(VL, true, UserTreeIdx);
1765       DEBUG(dbgs() << "SLP: added a vector of GEPs.\n");
1766       for (unsigned i = 0, e = 2; i < e; ++i) {
1767         ValueList Operands;
1768         // Prepare the operand vector.
1769         for (Value *j : VL)
1770           Operands.push_back(cast<Instruction>(j)->getOperand(i));
1771 
1772         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1773       }
1774       return;
1775     }
1776     case Instruction::Store: {
1777       // Check if the stores are consecutive or of we need to swizzle them.
1778       for (unsigned i = 0, e = VL.size() - 1; i < e; ++i)
1779         if (!isConsecutiveAccess(VL[i], VL[i + 1], *DL, *SE)) {
1780           BS.cancelScheduling(VL, VL0);
1781           newTreeEntry(VL, false, UserTreeIdx);
1782           DEBUG(dbgs() << "SLP: Non-consecutive store.\n");
1783           return;
1784         }
1785 
1786       newTreeEntry(VL, true, UserTreeIdx);
1787       DEBUG(dbgs() << "SLP: added a vector of stores.\n");
1788 
1789       ValueList Operands;
1790       for (Value *j : VL)
1791         Operands.push_back(cast<Instruction>(j)->getOperand(0));
1792 
1793       buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1794       return;
1795     }
1796     case Instruction::Call: {
1797       // Check if the calls are all to the same vectorizable intrinsic.
1798       CallInst *CI = cast<CallInst>(VL0);
1799       // Check if this is an Intrinsic call or something that can be
1800       // represented by an intrinsic call
1801       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
1802       if (!isTriviallyVectorizable(ID)) {
1803         BS.cancelScheduling(VL, VL0);
1804         newTreeEntry(VL, false, UserTreeIdx);
1805         DEBUG(dbgs() << "SLP: Non-vectorizable call.\n");
1806         return;
1807       }
1808       Function *Int = CI->getCalledFunction();
1809       Value *A1I = nullptr;
1810       if (hasVectorInstrinsicScalarOpd(ID, 1))
1811         A1I = CI->getArgOperand(1);
1812       for (unsigned i = 1, e = VL.size(); i != e; ++i) {
1813         CallInst *CI2 = dyn_cast<CallInst>(VL[i]);
1814         if (!CI2 || CI2->getCalledFunction() != Int ||
1815             getVectorIntrinsicIDForCall(CI2, TLI) != ID ||
1816             !CI->hasIdenticalOperandBundleSchema(*CI2)) {
1817           BS.cancelScheduling(VL, VL0);
1818           newTreeEntry(VL, false, UserTreeIdx);
1819           DEBUG(dbgs() << "SLP: mismatched calls:" << *CI << "!=" << *VL[i]
1820                        << "\n");
1821           return;
1822         }
1823         // ctlz,cttz and powi are special intrinsics whose second argument
1824         // should be same in order for them to be vectorized.
1825         if (hasVectorInstrinsicScalarOpd(ID, 1)) {
1826           Value *A1J = CI2->getArgOperand(1);
1827           if (A1I != A1J) {
1828             BS.cancelScheduling(VL, VL0);
1829             newTreeEntry(VL, false, UserTreeIdx);
1830             DEBUG(dbgs() << "SLP: mismatched arguments in call:" << *CI
1831                          << " argument "<< A1I<<"!=" << A1J
1832                          << "\n");
1833             return;
1834           }
1835         }
1836         // Verify that the bundle operands are identical between the two calls.
1837         if (CI->hasOperandBundles() &&
1838             !std::equal(CI->op_begin() + CI->getBundleOperandsStartIndex(),
1839                         CI->op_begin() + CI->getBundleOperandsEndIndex(),
1840                         CI2->op_begin() + CI2->getBundleOperandsStartIndex())) {
1841           BS.cancelScheduling(VL, VL0);
1842           newTreeEntry(VL, false, UserTreeIdx);
1843           DEBUG(dbgs() << "SLP: mismatched bundle operands in calls:" << *CI << "!="
1844                        << *VL[i] << '\n');
1845           return;
1846         }
1847       }
1848 
1849       newTreeEntry(VL, true, UserTreeIdx);
1850       for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) {
1851         ValueList Operands;
1852         // Prepare the operand vector.
1853         for (Value *j : VL) {
1854           CallInst *CI2 = dyn_cast<CallInst>(j);
1855           Operands.push_back(CI2->getArgOperand(i));
1856         }
1857         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1858       }
1859       return;
1860     }
1861     case Instruction::ShuffleVector:
1862       // If this is not an alternate sequence of opcode like add-sub
1863       // then do not vectorize this instruction.
1864       if (!S.IsAltShuffle) {
1865         BS.cancelScheduling(VL, VL0);
1866         newTreeEntry(VL, false, UserTreeIdx);
1867         DEBUG(dbgs() << "SLP: ShuffleVector are not vectorized.\n");
1868         return;
1869       }
1870       newTreeEntry(VL, true, UserTreeIdx);
1871       DEBUG(dbgs() << "SLP: added a ShuffleVector op.\n");
1872 
1873       // Reorder operands if reordering would enable vectorization.
1874       if (isa<BinaryOperator>(VL0)) {
1875         ValueList Left, Right;
1876         reorderAltShuffleOperands(S.Opcode, VL, Left, Right);
1877         buildTree_rec(Left, Depth + 1, UserTreeIdx);
1878         buildTree_rec(Right, Depth + 1, UserTreeIdx);
1879         return;
1880       }
1881 
1882       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1883         ValueList Operands;
1884         // Prepare the operand vector.
1885         for (Value *j : VL)
1886           Operands.push_back(cast<Instruction>(j)->getOperand(i));
1887 
1888         buildTree_rec(Operands, Depth + 1, UserTreeIdx);
1889       }
1890       return;
1891 
1892     default:
1893       BS.cancelScheduling(VL, VL0);
1894       newTreeEntry(VL, false, UserTreeIdx);
1895       DEBUG(dbgs() << "SLP: Gathering unknown instruction.\n");
1896       return;
1897   }
1898 }
1899 
1900 unsigned BoUpSLP::canMapToVector(Type *T, const DataLayout &DL) const {
1901   unsigned N;
1902   Type *EltTy;
1903   auto *ST = dyn_cast<StructType>(T);
1904   if (ST) {
1905     N = ST->getNumElements();
1906     EltTy = *ST->element_begin();
1907   } else {
1908     N = cast<ArrayType>(T)->getNumElements();
1909     EltTy = cast<ArrayType>(T)->getElementType();
1910   }
1911   if (!isValidElementType(EltTy))
1912     return 0;
1913   uint64_t VTSize = DL.getTypeStoreSizeInBits(VectorType::get(EltTy, N));
1914   if (VTSize < MinVecRegSize || VTSize > MaxVecRegSize || VTSize != DL.getTypeStoreSizeInBits(T))
1915     return 0;
1916   if (ST) {
1917     // Check that struct is homogeneous.
1918     for (const auto *Ty : ST->elements())
1919       if (Ty != EltTy)
1920         return 0;
1921   }
1922   return N;
1923 }
1924 
1925 bool BoUpSLP::canReuseExtract(ArrayRef<Value *> VL, Value *OpValue) const {
1926   Instruction *E0 = cast<Instruction>(OpValue);
1927   assert(E0->getOpcode() == Instruction::ExtractElement ||
1928          E0->getOpcode() == Instruction::ExtractValue);
1929   assert(E0->getOpcode() == getSameOpcode(VL).Opcode && "Invalid opcode");
1930   // Check if all of the extracts come from the same vector and from the
1931   // correct offset.
1932   Value *Vec = E0->getOperand(0);
1933 
1934   // We have to extract from a vector/aggregate with the same number of elements.
1935   unsigned NElts;
1936   if (E0->getOpcode() == Instruction::ExtractValue) {
1937     const DataLayout &DL = E0->getModule()->getDataLayout();
1938     NElts = canMapToVector(Vec->getType(), DL);
1939     if (!NElts)
1940       return false;
1941     // Check if load can be rewritten as load of vector.
1942     LoadInst *LI = dyn_cast<LoadInst>(Vec);
1943     if (!LI || !LI->isSimple() || !LI->hasNUses(VL.size()))
1944       return false;
1945   } else {
1946     NElts = Vec->getType()->getVectorNumElements();
1947   }
1948 
1949   if (NElts != VL.size())
1950     return false;
1951 
1952   // Check that all of the indices extract from the correct offset.
1953   for (unsigned I = 0, E = VL.size(); I < E; ++I) {
1954     Instruction *Inst = cast<Instruction>(VL[I]);
1955     if (!matchExtractIndex(Inst, I, Inst->getOpcode()))
1956       return false;
1957     if (Inst->getOperand(0) != Vec)
1958       return false;
1959   }
1960 
1961   return true;
1962 }
1963 
1964 bool BoUpSLP::areAllUsersVectorized(Instruction *I) const {
1965   return I->hasOneUse() ||
1966          std::all_of(I->user_begin(), I->user_end(), [this](User *U) {
1967            return ScalarToTreeEntry.count(U) > 0;
1968          });
1969 }
1970 
1971 int BoUpSLP::getEntryCost(TreeEntry *E) {
1972   ArrayRef<Value*> VL = E->Scalars;
1973 
1974   Type *ScalarTy = VL[0]->getType();
1975   if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1976     ScalarTy = SI->getValueOperand()->getType();
1977   else if (CmpInst *CI = dyn_cast<CmpInst>(VL[0]))
1978     ScalarTy = CI->getOperand(0)->getType();
1979   VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1980 
1981   // If we have computed a smaller type for the expression, update VecTy so
1982   // that the costs will be accurate.
1983   if (MinBWs.count(VL[0]))
1984     VecTy = VectorType::get(
1985         IntegerType::get(F->getContext(), MinBWs[VL[0]].first), VL.size());
1986 
1987   if (E->NeedToGather) {
1988     if (allConstant(VL))
1989       return 0;
1990     if (isSplat(VL)) {
1991       return TTI->getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy, 0);
1992     }
1993     if (getSameOpcode(VL).Opcode == Instruction::ExtractElement) {
1994       Optional<TargetTransformInfo::ShuffleKind> ShuffleKind = isShuffle(VL);
1995       if (ShuffleKind.hasValue()) {
1996         int Cost = TTI->getShuffleCost(ShuffleKind.getValue(), VecTy);
1997         for (auto *V : VL) {
1998           // If all users of instruction are going to be vectorized and this
1999           // instruction itself is not going to be vectorized, consider this
2000           // instruction as dead and remove its cost from the final cost of the
2001           // vectorized tree.
2002           if (areAllUsersVectorized(cast<Instruction>(V)) &&
2003               !ScalarToTreeEntry.count(V)) {
2004             auto *IO = cast<ConstantInt>(
2005                 cast<ExtractElementInst>(V)->getIndexOperand());
2006             Cost -= TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy,
2007                                             IO->getZExtValue());
2008           }
2009         }
2010         return Cost;
2011       }
2012     }
2013     return getGatherCost(E->Scalars);
2014   }
2015   InstructionsState S = getSameOpcode(VL);
2016   assert(S.Opcode && allSameType(VL) && allSameBlock(VL) && "Invalid VL");
2017   Instruction *VL0 = cast<Instruction>(S.OpValue);
2018   unsigned ShuffleOrOp = S.IsAltShuffle ?
2019                (unsigned) Instruction::ShuffleVector : S.Opcode;
2020   switch (ShuffleOrOp) {
2021     case Instruction::PHI:
2022       return 0;
2023 
2024     case Instruction::ExtractValue:
2025     case Instruction::ExtractElement:
2026       if (canReuseExtract(VL, S.OpValue)) {
2027         int DeadCost = 0;
2028         for (unsigned i = 0, e = VL.size(); i < e; ++i) {
2029           Instruction *E = cast<Instruction>(VL[i]);
2030           // If all users are going to be vectorized, instruction can be
2031           // considered as dead.
2032           // The same, if have only one user, it will be vectorized for sure.
2033           if (areAllUsersVectorized(E))
2034             // Take credit for instruction that will become dead.
2035             DeadCost +=
2036                 TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, i);
2037         }
2038         return -DeadCost;
2039       }
2040       return getGatherCost(VecTy);
2041 
2042     case Instruction::ZExt:
2043     case Instruction::SExt:
2044     case Instruction::FPToUI:
2045     case Instruction::FPToSI:
2046     case Instruction::FPExt:
2047     case Instruction::PtrToInt:
2048     case Instruction::IntToPtr:
2049     case Instruction::SIToFP:
2050     case Instruction::UIToFP:
2051     case Instruction::Trunc:
2052     case Instruction::FPTrunc:
2053     case Instruction::BitCast: {
2054       Type *SrcTy = VL0->getOperand(0)->getType();
2055 
2056       // Calculate the cost of this instruction.
2057       int ScalarCost = VL.size() * TTI->getCastInstrCost(VL0->getOpcode(),
2058                                                          VL0->getType(), SrcTy, VL0);
2059 
2060       VectorType *SrcVecTy = VectorType::get(SrcTy, VL.size());
2061       int VecCost = TTI->getCastInstrCost(VL0->getOpcode(), VecTy, SrcVecTy, VL0);
2062       return VecCost - ScalarCost;
2063     }
2064     case Instruction::FCmp:
2065     case Instruction::ICmp:
2066     case Instruction::Select: {
2067       // Calculate the cost of this instruction.
2068       VectorType *MaskTy = VectorType::get(Builder.getInt1Ty(), VL.size());
2069       int ScalarCost = VecTy->getNumElements() *
2070           TTI->getCmpSelInstrCost(S.Opcode, ScalarTy, Builder.getInt1Ty(), VL0);
2071       int VecCost = TTI->getCmpSelInstrCost(S.Opcode, VecTy, MaskTy, VL0);
2072       return VecCost - ScalarCost;
2073     }
2074     case Instruction::Add:
2075     case Instruction::FAdd:
2076     case Instruction::Sub:
2077     case Instruction::FSub:
2078     case Instruction::Mul:
2079     case Instruction::FMul:
2080     case Instruction::UDiv:
2081     case Instruction::SDiv:
2082     case Instruction::FDiv:
2083     case Instruction::URem:
2084     case Instruction::SRem:
2085     case Instruction::FRem:
2086     case Instruction::Shl:
2087     case Instruction::LShr:
2088     case Instruction::AShr:
2089     case Instruction::And:
2090     case Instruction::Or:
2091     case Instruction::Xor: {
2092       // Certain instructions can be cheaper to vectorize if they have a
2093       // constant second vector operand.
2094       TargetTransformInfo::OperandValueKind Op1VK =
2095           TargetTransformInfo::OK_AnyValue;
2096       TargetTransformInfo::OperandValueKind Op2VK =
2097           TargetTransformInfo::OK_UniformConstantValue;
2098       TargetTransformInfo::OperandValueProperties Op1VP =
2099           TargetTransformInfo::OP_None;
2100       TargetTransformInfo::OperandValueProperties Op2VP =
2101           TargetTransformInfo::OP_None;
2102 
2103       // If all operands are exactly the same ConstantInt then set the
2104       // operand kind to OK_UniformConstantValue.
2105       // If instead not all operands are constants, then set the operand kind
2106       // to OK_AnyValue. If all operands are constants but not the same,
2107       // then set the operand kind to OK_NonUniformConstantValue.
2108       ConstantInt *CInt = nullptr;
2109       for (unsigned i = 0; i < VL.size(); ++i) {
2110         const Instruction *I = cast<Instruction>(VL[i]);
2111         if (!isa<ConstantInt>(I->getOperand(1))) {
2112           Op2VK = TargetTransformInfo::OK_AnyValue;
2113           break;
2114         }
2115         if (i == 0) {
2116           CInt = cast<ConstantInt>(I->getOperand(1));
2117           continue;
2118         }
2119         if (Op2VK == TargetTransformInfo::OK_UniformConstantValue &&
2120             CInt != cast<ConstantInt>(I->getOperand(1)))
2121           Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
2122       }
2123       // FIXME: Currently cost of model modification for division by power of
2124       // 2 is handled for X86 and AArch64. Add support for other targets.
2125       if (Op2VK == TargetTransformInfo::OK_UniformConstantValue && CInt &&
2126           CInt->getValue().isPowerOf2())
2127         Op2VP = TargetTransformInfo::OP_PowerOf2;
2128 
2129       SmallVector<const Value *, 4> Operands(VL0->operand_values());
2130       int ScalarCost =
2131           VecTy->getNumElements() *
2132           TTI->getArithmeticInstrCost(S.Opcode, ScalarTy, Op1VK, Op2VK, Op1VP,
2133                                       Op2VP, Operands);
2134       int VecCost = TTI->getArithmeticInstrCost(S.Opcode, VecTy, Op1VK, Op2VK,
2135                                                 Op1VP, Op2VP, Operands);
2136       return VecCost - ScalarCost;
2137     }
2138     case Instruction::GetElementPtr: {
2139       TargetTransformInfo::OperandValueKind Op1VK =
2140           TargetTransformInfo::OK_AnyValue;
2141       TargetTransformInfo::OperandValueKind Op2VK =
2142           TargetTransformInfo::OK_UniformConstantValue;
2143 
2144       int ScalarCost =
2145           VecTy->getNumElements() *
2146           TTI->getArithmeticInstrCost(Instruction::Add, ScalarTy, Op1VK, Op2VK);
2147       int VecCost =
2148           TTI->getArithmeticInstrCost(Instruction::Add, VecTy, Op1VK, Op2VK);
2149 
2150       return VecCost - ScalarCost;
2151     }
2152     case Instruction::Load: {
2153       // Cost of wide load - cost of scalar loads.
2154       unsigned alignment = dyn_cast<LoadInst>(VL0)->getAlignment();
2155       int ScalarLdCost = VecTy->getNumElements() *
2156           TTI->getMemoryOpCost(Instruction::Load, ScalarTy, alignment, 0, VL0);
2157       int VecLdCost = TTI->getMemoryOpCost(Instruction::Load,
2158                                            VecTy, alignment, 0, VL0);
2159       return VecLdCost - ScalarLdCost;
2160     }
2161     case Instruction::Store: {
2162       // We know that we can merge the stores. Calculate the cost.
2163       unsigned alignment = dyn_cast<StoreInst>(VL0)->getAlignment();
2164       int ScalarStCost = VecTy->getNumElements() *
2165           TTI->getMemoryOpCost(Instruction::Store, ScalarTy, alignment, 0, VL0);
2166       int VecStCost = TTI->getMemoryOpCost(Instruction::Store,
2167                                            VecTy, alignment, 0, VL0);
2168       return VecStCost - ScalarStCost;
2169     }
2170     case Instruction::Call: {
2171       CallInst *CI = cast<CallInst>(VL0);
2172       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
2173 
2174       // Calculate the cost of the scalar and vector calls.
2175       SmallVector<Type*, 4> ScalarTys;
2176       for (unsigned op = 0, opc = CI->getNumArgOperands(); op!= opc; ++op)
2177         ScalarTys.push_back(CI->getArgOperand(op)->getType());
2178 
2179       FastMathFlags FMF;
2180       if (auto *FPMO = dyn_cast<FPMathOperator>(CI))
2181         FMF = FPMO->getFastMathFlags();
2182 
2183       int ScalarCallCost = VecTy->getNumElements() *
2184           TTI->getIntrinsicInstrCost(ID, ScalarTy, ScalarTys, FMF);
2185 
2186       SmallVector<Value *, 4> Args(CI->arg_operands());
2187       int VecCallCost = TTI->getIntrinsicInstrCost(ID, CI->getType(), Args, FMF,
2188                                                    VecTy->getNumElements());
2189 
2190       DEBUG(dbgs() << "SLP: Call cost "<< VecCallCost - ScalarCallCost
2191             << " (" << VecCallCost  << "-" <<  ScalarCallCost << ")"
2192             << " for " << *CI << "\n");
2193 
2194       return VecCallCost - ScalarCallCost;
2195     }
2196     case Instruction::ShuffleVector: {
2197       TargetTransformInfo::OperandValueKind Op1VK =
2198           TargetTransformInfo::OK_AnyValue;
2199       TargetTransformInfo::OperandValueKind Op2VK =
2200           TargetTransformInfo::OK_AnyValue;
2201       int ScalarCost = 0;
2202       int VecCost = 0;
2203       for (Value *i : VL) {
2204         Instruction *I = cast<Instruction>(i);
2205         if (!I)
2206           break;
2207         ScalarCost +=
2208             TTI->getArithmeticInstrCost(I->getOpcode(), ScalarTy, Op1VK, Op2VK);
2209       }
2210       // VecCost is equal to sum of the cost of creating 2 vectors
2211       // and the cost of creating shuffle.
2212       Instruction *I0 = cast<Instruction>(VL[0]);
2213       VecCost =
2214           TTI->getArithmeticInstrCost(I0->getOpcode(), VecTy, Op1VK, Op2VK);
2215       Instruction *I1 = cast<Instruction>(VL[1]);
2216       VecCost +=
2217           TTI->getArithmeticInstrCost(I1->getOpcode(), VecTy, Op1VK, Op2VK);
2218       VecCost +=
2219           TTI->getShuffleCost(TargetTransformInfo::SK_Alternate, VecTy, 0);
2220       return VecCost - ScalarCost;
2221     }
2222     default:
2223       llvm_unreachable("Unknown instruction");
2224   }
2225 }
2226 
2227 bool BoUpSLP::isFullyVectorizableTinyTree() {
2228   DEBUG(dbgs() << "SLP: Check whether the tree with height " <<
2229         VectorizableTree.size() << " is fully vectorizable .\n");
2230 
2231   // We only handle trees of heights 1 and 2.
2232   if (VectorizableTree.size() == 1 && !VectorizableTree[0].NeedToGather)
2233     return true;
2234 
2235   if (VectorizableTree.size() != 2)
2236     return false;
2237 
2238   // Handle splat and all-constants stores.
2239   if (!VectorizableTree[0].NeedToGather &&
2240       (allConstant(VectorizableTree[1].Scalars) ||
2241        isSplat(VectorizableTree[1].Scalars)))
2242     return true;
2243 
2244   // Gathering cost would be too much for tiny trees.
2245   if (VectorizableTree[0].NeedToGather || VectorizableTree[1].NeedToGather)
2246     return false;
2247 
2248   return true;
2249 }
2250 
2251 bool BoUpSLP::isTreeTinyAndNotFullyVectorizable() {
2252   // We can vectorize the tree if its size is greater than or equal to the
2253   // minimum size specified by the MinTreeSize command line option.
2254   if (VectorizableTree.size() >= MinTreeSize)
2255     return false;
2256 
2257   // If we have a tiny tree (a tree whose size is less than MinTreeSize), we
2258   // can vectorize it if we can prove it fully vectorizable.
2259   if (isFullyVectorizableTinyTree())
2260     return false;
2261 
2262   assert(VectorizableTree.empty()
2263              ? ExternalUses.empty()
2264              : true && "We shouldn't have any external users");
2265 
2266   // Otherwise, we can't vectorize the tree. It is both tiny and not fully
2267   // vectorizable.
2268   return true;
2269 }
2270 
2271 int BoUpSLP::getSpillCost() {
2272   // Walk from the bottom of the tree to the top, tracking which values are
2273   // live. When we see a call instruction that is not part of our tree,
2274   // query TTI to see if there is a cost to keeping values live over it
2275   // (for example, if spills and fills are required).
2276   unsigned BundleWidth = VectorizableTree.front().Scalars.size();
2277   int Cost = 0;
2278 
2279   SmallPtrSet<Instruction*, 4> LiveValues;
2280   Instruction *PrevInst = nullptr;
2281 
2282   for (const auto &N : VectorizableTree) {
2283     Instruction *Inst = dyn_cast<Instruction>(N.Scalars[0]);
2284     if (!Inst)
2285       continue;
2286 
2287     if (!PrevInst) {
2288       PrevInst = Inst;
2289       continue;
2290     }
2291 
2292     // Update LiveValues.
2293     LiveValues.erase(PrevInst);
2294     for (auto &J : PrevInst->operands()) {
2295       if (isa<Instruction>(&*J) && getTreeEntry(&*J))
2296         LiveValues.insert(cast<Instruction>(&*J));
2297     }
2298 
2299     DEBUG(
2300       dbgs() << "SLP: #LV: " << LiveValues.size();
2301       for (auto *X : LiveValues)
2302         dbgs() << " " << X->getName();
2303       dbgs() << ", Looking at ";
2304       Inst->dump();
2305       );
2306 
2307     // Now find the sequence of instructions between PrevInst and Inst.
2308     BasicBlock::reverse_iterator InstIt = ++Inst->getIterator().getReverse(),
2309                                  PrevInstIt =
2310                                      PrevInst->getIterator().getReverse();
2311     while (InstIt != PrevInstIt) {
2312       if (PrevInstIt == PrevInst->getParent()->rend()) {
2313         PrevInstIt = Inst->getParent()->rbegin();
2314         continue;
2315       }
2316 
2317       if (isa<CallInst>(&*PrevInstIt) && &*PrevInstIt != PrevInst) {
2318         SmallVector<Type*, 4> V;
2319         for (auto *II : LiveValues)
2320           V.push_back(VectorType::get(II->getType(), BundleWidth));
2321         Cost += TTI->getCostOfKeepingLiveOverCall(V);
2322       }
2323 
2324       ++PrevInstIt;
2325     }
2326 
2327     PrevInst = Inst;
2328   }
2329 
2330   return Cost;
2331 }
2332 
2333 int BoUpSLP::getTreeCost() {
2334   int Cost = 0;
2335   DEBUG(dbgs() << "SLP: Calculating cost for tree of size " <<
2336         VectorizableTree.size() << ".\n");
2337 
2338   unsigned BundleWidth = VectorizableTree[0].Scalars.size();
2339 
2340   for (TreeEntry &TE : VectorizableTree) {
2341     int C = getEntryCost(&TE);
2342     DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle that starts with "
2343                  << *TE.Scalars[0] << ".\n");
2344     Cost += C;
2345   }
2346 
2347   SmallSet<Value *, 16> ExtractCostCalculated;
2348   int ExtractCost = 0;
2349   for (ExternalUser &EU : ExternalUses) {
2350     // We only add extract cost once for the same scalar.
2351     if (!ExtractCostCalculated.insert(EU.Scalar).second)
2352       continue;
2353 
2354     // Uses by ephemeral values are free (because the ephemeral value will be
2355     // removed prior to code generation, and so the extraction will be
2356     // removed as well).
2357     if (EphValues.count(EU.User))
2358       continue;
2359 
2360     // If we plan to rewrite the tree in a smaller type, we will need to sign
2361     // extend the extracted value back to the original type. Here, we account
2362     // for the extract and the added cost of the sign extend if needed.
2363     auto *VecTy = VectorType::get(EU.Scalar->getType(), BundleWidth);
2364     auto *ScalarRoot = VectorizableTree[0].Scalars[0];
2365     if (MinBWs.count(ScalarRoot)) {
2366       auto *MinTy = IntegerType::get(F->getContext(), MinBWs[ScalarRoot].first);
2367       auto Extend =
2368           MinBWs[ScalarRoot].second ? Instruction::SExt : Instruction::ZExt;
2369       VecTy = VectorType::get(MinTy, BundleWidth);
2370       ExtractCost += TTI->getExtractWithExtendCost(Extend, EU.Scalar->getType(),
2371                                                    VecTy, EU.Lane);
2372     } else {
2373       ExtractCost +=
2374           TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, EU.Lane);
2375     }
2376   }
2377 
2378   int SpillCost = getSpillCost();
2379   Cost += SpillCost + ExtractCost;
2380 
2381   std::string Str;
2382   {
2383     raw_string_ostream OS(Str);
2384     OS << "SLP: Spill Cost = " << SpillCost << ".\n"
2385        << "SLP: Extract Cost = " << ExtractCost << ".\n"
2386        << "SLP: Total Cost = " << Cost << ".\n";
2387   }
2388   DEBUG(dbgs() << Str);
2389 
2390   if (ViewSLPTree)
2391     ViewGraph(this, "SLP" + F->getName(), false, Str);
2392 
2393   return Cost;
2394 }
2395 
2396 int BoUpSLP::getGatherCost(Type *Ty) {
2397   int Cost = 0;
2398   for (unsigned i = 0, e = cast<VectorType>(Ty)->getNumElements(); i < e; ++i)
2399     Cost += TTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
2400   return Cost;
2401 }
2402 
2403 int BoUpSLP::getGatherCost(ArrayRef<Value *> VL) {
2404   // Find the type of the operands in VL.
2405   Type *ScalarTy = VL[0]->getType();
2406   if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
2407     ScalarTy = SI->getValueOperand()->getType();
2408   VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
2409   // Find the cost of inserting/extracting values from the vector.
2410   return getGatherCost(VecTy);
2411 }
2412 
2413 // Reorder commutative operations in alternate shuffle if the resulting vectors
2414 // are consecutive loads. This would allow us to vectorize the tree.
2415 // If we have something like-
2416 // load a[0] - load b[0]
2417 // load b[1] + load a[1]
2418 // load a[2] - load b[2]
2419 // load a[3] + load b[3]
2420 // Reordering the second load b[1]  load a[1] would allow us to vectorize this
2421 // code.
2422 void BoUpSLP::reorderAltShuffleOperands(unsigned Opcode, ArrayRef<Value *> VL,
2423                                         SmallVectorImpl<Value *> &Left,
2424                                         SmallVectorImpl<Value *> &Right) {
2425   // Push left and right operands of binary operation into Left and Right
2426   unsigned AltOpcode = getAltOpcode(Opcode);
2427   (void)AltOpcode;
2428   for (Value *V : VL) {
2429     auto *I = cast<Instruction>(V);
2430     assert(sameOpcodeOrAlt(Opcode, AltOpcode, I->getOpcode()) &&
2431            "Incorrect instruction in vector");
2432     Left.push_back(I->getOperand(0));
2433     Right.push_back(I->getOperand(1));
2434   }
2435 
2436   // Reorder if we have a commutative operation and consecutive access
2437   // are on either side of the alternate instructions.
2438   for (unsigned j = 0; j < VL.size() - 1; ++j) {
2439     if (LoadInst *L = dyn_cast<LoadInst>(Left[j])) {
2440       if (LoadInst *L1 = dyn_cast<LoadInst>(Right[j + 1])) {
2441         Instruction *VL1 = cast<Instruction>(VL[j]);
2442         Instruction *VL2 = cast<Instruction>(VL[j + 1]);
2443         if (VL1->isCommutative() && isConsecutiveAccess(L, L1, *DL, *SE)) {
2444           std::swap(Left[j], Right[j]);
2445           continue;
2446         } else if (VL2->isCommutative() &&
2447                    isConsecutiveAccess(L, L1, *DL, *SE)) {
2448           std::swap(Left[j + 1], Right[j + 1]);
2449           continue;
2450         }
2451         // else unchanged
2452       }
2453     }
2454     if (LoadInst *L = dyn_cast<LoadInst>(Right[j])) {
2455       if (LoadInst *L1 = dyn_cast<LoadInst>(Left[j + 1])) {
2456         Instruction *VL1 = cast<Instruction>(VL[j]);
2457         Instruction *VL2 = cast<Instruction>(VL[j + 1]);
2458         if (VL1->isCommutative() && isConsecutiveAccess(L, L1, *DL, *SE)) {
2459           std::swap(Left[j], Right[j]);
2460           continue;
2461         } else if (VL2->isCommutative() &&
2462                    isConsecutiveAccess(L, L1, *DL, *SE)) {
2463           std::swap(Left[j + 1], Right[j + 1]);
2464           continue;
2465         }
2466         // else unchanged
2467       }
2468     }
2469   }
2470 }
2471 
2472 // Return true if I should be commuted before adding it's left and right
2473 // operands to the arrays Left and Right.
2474 //
2475 // The vectorizer is trying to either have all elements one side being
2476 // instruction with the same opcode to enable further vectorization, or having
2477 // a splat to lower the vectorizing cost.
2478 static bool shouldReorderOperands(
2479     int i, unsigned Opcode, Instruction &I, ArrayRef<Value *> Left,
2480     ArrayRef<Value *> Right, bool AllSameOpcodeLeft, bool AllSameOpcodeRight,
2481     bool SplatLeft, bool SplatRight, Value *&VLeft, Value *&VRight) {
2482   VLeft = I.getOperand(0);
2483   VRight = I.getOperand(1);
2484   // If we have "SplatRight", try to see if commuting is needed to preserve it.
2485   if (SplatRight) {
2486     if (VRight == Right[i - 1])
2487       // Preserve SplatRight
2488       return false;
2489     if (VLeft == Right[i - 1]) {
2490       // Commuting would preserve SplatRight, but we don't want to break
2491       // SplatLeft either, i.e. preserve the original order if possible.
2492       // (FIXME: why do we care?)
2493       if (SplatLeft && VLeft == Left[i - 1])
2494         return false;
2495       return true;
2496     }
2497   }
2498   // Symmetrically handle Right side.
2499   if (SplatLeft) {
2500     if (VLeft == Left[i - 1])
2501       // Preserve SplatLeft
2502       return false;
2503     if (VRight == Left[i - 1])
2504       return true;
2505   }
2506 
2507   Instruction *ILeft = dyn_cast<Instruction>(VLeft);
2508   Instruction *IRight = dyn_cast<Instruction>(VRight);
2509 
2510   // If we have "AllSameOpcodeRight", try to see if the left operands preserves
2511   // it and not the right, in this case we want to commute.
2512   if (AllSameOpcodeRight) {
2513     unsigned RightPrevOpcode = cast<Instruction>(Right[i - 1])->getOpcode();
2514     if (IRight && RightPrevOpcode == IRight->getOpcode())
2515       // Do not commute, a match on the right preserves AllSameOpcodeRight
2516       return false;
2517     if (ILeft && RightPrevOpcode == ILeft->getOpcode()) {
2518       // We have a match and may want to commute, but first check if there is
2519       // not also a match on the existing operands on the Left to preserve
2520       // AllSameOpcodeLeft, i.e. preserve the original order if possible.
2521       // (FIXME: why do we care?)
2522       if (AllSameOpcodeLeft && ILeft &&
2523           cast<Instruction>(Left[i - 1])->getOpcode() == ILeft->getOpcode())
2524         return false;
2525       return true;
2526     }
2527   }
2528   // Symmetrically handle Left side.
2529   if (AllSameOpcodeLeft) {
2530     unsigned LeftPrevOpcode = cast<Instruction>(Left[i - 1])->getOpcode();
2531     if (ILeft && LeftPrevOpcode == ILeft->getOpcode())
2532       return false;
2533     if (IRight && LeftPrevOpcode == IRight->getOpcode())
2534       return true;
2535   }
2536   return false;
2537 }
2538 
2539 void BoUpSLP::reorderInputsAccordingToOpcode(unsigned Opcode,
2540                                              ArrayRef<Value *> VL,
2541                                              SmallVectorImpl<Value *> &Left,
2542                                              SmallVectorImpl<Value *> &Right) {
2543   if (!VL.empty()) {
2544     // Peel the first iteration out of the loop since there's nothing
2545     // interesting to do anyway and it simplifies the checks in the loop.
2546     auto *I = cast<Instruction>(VL[0]);
2547     Value *VLeft = I->getOperand(0);
2548     Value *VRight = I->getOperand(1);
2549     if (!isa<Instruction>(VRight) && isa<Instruction>(VLeft))
2550       // Favor having instruction to the right. FIXME: why?
2551       std::swap(VLeft, VRight);
2552     Left.push_back(VLeft);
2553     Right.push_back(VRight);
2554   }
2555 
2556   // Keep track if we have instructions with all the same opcode on one side.
2557   bool AllSameOpcodeLeft = isa<Instruction>(Left[0]);
2558   bool AllSameOpcodeRight = isa<Instruction>(Right[0]);
2559   // Keep track if we have one side with all the same value (broadcast).
2560   bool SplatLeft = true;
2561   bool SplatRight = true;
2562 
2563   for (unsigned i = 1, e = VL.size(); i != e; ++i) {
2564     Instruction *I = cast<Instruction>(VL[i]);
2565     assert(((I->getOpcode() == Opcode && I->isCommutative()) ||
2566             (I->getOpcode() != Opcode && Instruction::isCommutative(Opcode))) &&
2567            "Can only process commutative instruction");
2568     // Commute to favor either a splat or maximizing having the same opcodes on
2569     // one side.
2570     Value *VLeft;
2571     Value *VRight;
2572     if (shouldReorderOperands(i, Opcode, *I, Left, Right, AllSameOpcodeLeft,
2573                               AllSameOpcodeRight, SplatLeft, SplatRight, VLeft,
2574                               VRight)) {
2575       Left.push_back(VRight);
2576       Right.push_back(VLeft);
2577     } else {
2578       Left.push_back(VLeft);
2579       Right.push_back(VRight);
2580     }
2581     // Update Splat* and AllSameOpcode* after the insertion.
2582     SplatRight = SplatRight && (Right[i - 1] == Right[i]);
2583     SplatLeft = SplatLeft && (Left[i - 1] == Left[i]);
2584     AllSameOpcodeLeft = AllSameOpcodeLeft && isa<Instruction>(Left[i]) &&
2585                         (cast<Instruction>(Left[i - 1])->getOpcode() ==
2586                          cast<Instruction>(Left[i])->getOpcode());
2587     AllSameOpcodeRight = AllSameOpcodeRight && isa<Instruction>(Right[i]) &&
2588                          (cast<Instruction>(Right[i - 1])->getOpcode() ==
2589                           cast<Instruction>(Right[i])->getOpcode());
2590   }
2591 
2592   // If one operand end up being broadcast, return this operand order.
2593   if (SplatRight || SplatLeft)
2594     return;
2595 
2596   // Finally check if we can get longer vectorizable chain by reordering
2597   // without breaking the good operand order detected above.
2598   // E.g. If we have something like-
2599   // load a[0]  load b[0]
2600   // load b[1]  load a[1]
2601   // load a[2]  load b[2]
2602   // load a[3]  load b[3]
2603   // Reordering the second load b[1]  load a[1] would allow us to vectorize
2604   // this code and we still retain AllSameOpcode property.
2605   // FIXME: This load reordering might break AllSameOpcode in some rare cases
2606   // such as-
2607   // add a[0],c[0]  load b[0]
2608   // add a[1],c[2]  load b[1]
2609   // b[2]           load b[2]
2610   // add a[3],c[3]  load b[3]
2611   for (unsigned j = 0; j < VL.size() - 1; ++j) {
2612     if (LoadInst *L = dyn_cast<LoadInst>(Left[j])) {
2613       if (LoadInst *L1 = dyn_cast<LoadInst>(Right[j + 1])) {
2614         if (isConsecutiveAccess(L, L1, *DL, *SE)) {
2615           std::swap(Left[j + 1], Right[j + 1]);
2616           continue;
2617         }
2618       }
2619     }
2620     if (LoadInst *L = dyn_cast<LoadInst>(Right[j])) {
2621       if (LoadInst *L1 = dyn_cast<LoadInst>(Left[j + 1])) {
2622         if (isConsecutiveAccess(L, L1, *DL, *SE)) {
2623           std::swap(Left[j + 1], Right[j + 1]);
2624           continue;
2625         }
2626       }
2627     }
2628     // else unchanged
2629   }
2630 }
2631 
2632 void BoUpSLP::setInsertPointAfterBundle(ArrayRef<Value *> VL, Value *OpValue) {
2633   // Get the basic block this bundle is in. All instructions in the bundle
2634   // should be in this block.
2635   auto *Front = cast<Instruction>(OpValue);
2636   auto *BB = Front->getParent();
2637   const unsigned Opcode = cast<Instruction>(OpValue)->getOpcode();
2638   const unsigned AltOpcode = getAltOpcode(Opcode);
2639   assert(llvm::all_of(make_range(VL.begin(), VL.end()), [=](Value *V) -> bool {
2640     return !sameOpcodeOrAlt(Opcode, AltOpcode,
2641                             cast<Instruction>(V)->getOpcode()) ||
2642            cast<Instruction>(V)->getParent() == BB;
2643   }));
2644 
2645   // The last instruction in the bundle in program order.
2646   Instruction *LastInst = nullptr;
2647 
2648   // Find the last instruction. The common case should be that BB has been
2649   // scheduled, and the last instruction is VL.back(). So we start with
2650   // VL.back() and iterate over schedule data until we reach the end of the
2651   // bundle. The end of the bundle is marked by null ScheduleData.
2652   if (BlocksSchedules.count(BB)) {
2653     auto *Bundle =
2654         BlocksSchedules[BB]->getScheduleData(isOneOf(OpValue, VL.back()));
2655     if (Bundle && Bundle->isPartOfBundle())
2656       for (; Bundle; Bundle = Bundle->NextInBundle)
2657         if (Bundle->OpValue == Bundle->Inst)
2658           LastInst = Bundle->Inst;
2659   }
2660 
2661   // LastInst can still be null at this point if there's either not an entry
2662   // for BB in BlocksSchedules or there's no ScheduleData available for
2663   // VL.back(). This can be the case if buildTree_rec aborts for various
2664   // reasons (e.g., the maximum recursion depth is reached, the maximum region
2665   // size is reached, etc.). ScheduleData is initialized in the scheduling
2666   // "dry-run".
2667   //
2668   // If this happens, we can still find the last instruction by brute force. We
2669   // iterate forwards from Front (inclusive) until we either see all
2670   // instructions in the bundle or reach the end of the block. If Front is the
2671   // last instruction in program order, LastInst will be set to Front, and we
2672   // will visit all the remaining instructions in the block.
2673   //
2674   // One of the reasons we exit early from buildTree_rec is to place an upper
2675   // bound on compile-time. Thus, taking an additional compile-time hit here is
2676   // not ideal. However, this should be exceedingly rare since it requires that
2677   // we both exit early from buildTree_rec and that the bundle be out-of-order
2678   // (causing us to iterate all the way to the end of the block).
2679   if (!LastInst) {
2680     SmallPtrSet<Value *, 16> Bundle(VL.begin(), VL.end());
2681     for (auto &I : make_range(BasicBlock::iterator(Front), BB->end())) {
2682       if (Bundle.erase(&I) && sameOpcodeOrAlt(Opcode, AltOpcode, I.getOpcode()))
2683         LastInst = &I;
2684       if (Bundle.empty())
2685         break;
2686     }
2687   }
2688 
2689   // Set the insertion point after the last instruction in the bundle. Set the
2690   // debug location to Front.
2691   Builder.SetInsertPoint(BB, ++LastInst->getIterator());
2692   Builder.SetCurrentDebugLocation(Front->getDebugLoc());
2693 }
2694 
2695 Value *BoUpSLP::Gather(ArrayRef<Value *> VL, VectorType *Ty) {
2696   Value *Vec = UndefValue::get(Ty);
2697   // Generate the 'InsertElement' instruction.
2698   for (unsigned i = 0; i < Ty->getNumElements(); ++i) {
2699     Vec = Builder.CreateInsertElement(Vec, VL[i], Builder.getInt32(i));
2700     if (Instruction *Insrt = dyn_cast<Instruction>(Vec)) {
2701       GatherSeq.insert(Insrt);
2702       CSEBlocks.insert(Insrt->getParent());
2703 
2704       // Add to our 'need-to-extract' list.
2705       if (TreeEntry *E = getTreeEntry(VL[i])) {
2706         // Find which lane we need to extract.
2707         int FoundLane = -1;
2708         for (unsigned Lane = 0, LE = VL.size(); Lane != LE; ++Lane) {
2709           // Is this the lane of the scalar that we are looking for ?
2710           if (E->Scalars[Lane] == VL[i]) {
2711             FoundLane = Lane;
2712             break;
2713           }
2714         }
2715         assert(FoundLane >= 0 && "Could not find the correct lane");
2716         ExternalUses.push_back(ExternalUser(VL[i], Insrt, FoundLane));
2717       }
2718     }
2719   }
2720 
2721   return Vec;
2722 }
2723 
2724 Value *BoUpSLP::alreadyVectorized(ArrayRef<Value *> VL, Value *OpValue) const {
2725   if (const TreeEntry *En = getTreeEntry(OpValue)) {
2726     if (En->isSame(VL) && En->VectorizedValue)
2727       return En->VectorizedValue;
2728   }
2729   return nullptr;
2730 }
2731 
2732 Value *BoUpSLP::vectorizeTree(ArrayRef<Value *> VL) {
2733   InstructionsState S = getSameOpcode(VL);
2734   if (S.Opcode) {
2735     if (TreeEntry *E = getTreeEntry(S.OpValue)) {
2736       if (E->isSame(VL))
2737         return vectorizeTree(E);
2738     }
2739   }
2740 
2741   Type *ScalarTy = S.OpValue->getType();
2742   if (StoreInst *SI = dyn_cast<StoreInst>(S.OpValue))
2743     ScalarTy = SI->getValueOperand()->getType();
2744   VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
2745 
2746   return Gather(VL, VecTy);
2747 }
2748 
2749 Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
2750   IRBuilder<>::InsertPointGuard Guard(Builder);
2751 
2752   if (E->VectorizedValue) {
2753     DEBUG(dbgs() << "SLP: Diamond merged for " << *E->Scalars[0] << ".\n");
2754     return E->VectorizedValue;
2755   }
2756 
2757   InstructionsState S = getSameOpcode(E->Scalars);
2758   Instruction *VL0 = cast<Instruction>(E->Scalars[0]);
2759   Type *ScalarTy = VL0->getType();
2760   if (StoreInst *SI = dyn_cast<StoreInst>(VL0))
2761     ScalarTy = SI->getValueOperand()->getType();
2762   VectorType *VecTy = VectorType::get(ScalarTy, E->Scalars.size());
2763 
2764   if (E->NeedToGather) {
2765     setInsertPointAfterBundle(E->Scalars, VL0);
2766     auto *V = Gather(E->Scalars, VecTy);
2767     E->VectorizedValue = V;
2768     return V;
2769   }
2770 
2771   unsigned ShuffleOrOp = S.IsAltShuffle ?
2772            (unsigned) Instruction::ShuffleVector : S.Opcode;
2773   switch (ShuffleOrOp) {
2774     case Instruction::PHI: {
2775       PHINode *PH = dyn_cast<PHINode>(VL0);
2776       Builder.SetInsertPoint(PH->getParent()->getFirstNonPHI());
2777       Builder.SetCurrentDebugLocation(PH->getDebugLoc());
2778       PHINode *NewPhi = Builder.CreatePHI(VecTy, PH->getNumIncomingValues());
2779       E->VectorizedValue = NewPhi;
2780 
2781       // PHINodes may have multiple entries from the same block. We want to
2782       // visit every block once.
2783       SmallSet<BasicBlock*, 4> VisitedBBs;
2784 
2785       for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
2786         ValueList Operands;
2787         BasicBlock *IBB = PH->getIncomingBlock(i);
2788 
2789         if (!VisitedBBs.insert(IBB).second) {
2790           NewPhi->addIncoming(NewPhi->getIncomingValueForBlock(IBB), IBB);
2791           continue;
2792         }
2793 
2794         // Prepare the operand vector.
2795         for (Value *V : E->Scalars)
2796           Operands.push_back(cast<PHINode>(V)->getIncomingValueForBlock(IBB));
2797 
2798         Builder.SetInsertPoint(IBB->getTerminator());
2799         Builder.SetCurrentDebugLocation(PH->getDebugLoc());
2800         Value *Vec = vectorizeTree(Operands);
2801         NewPhi->addIncoming(Vec, IBB);
2802       }
2803 
2804       assert(NewPhi->getNumIncomingValues() == PH->getNumIncomingValues() &&
2805              "Invalid number of incoming values");
2806       return NewPhi;
2807     }
2808 
2809     case Instruction::ExtractElement: {
2810       if (canReuseExtract(E->Scalars, VL0)) {
2811         Value *V = VL0->getOperand(0);
2812         E->VectorizedValue = V;
2813         return V;
2814       }
2815       setInsertPointAfterBundle(E->Scalars, VL0);
2816       auto *V = Gather(E->Scalars, VecTy);
2817       E->VectorizedValue = V;
2818       return V;
2819     }
2820     case Instruction::ExtractValue: {
2821       if (canReuseExtract(E->Scalars, VL0)) {
2822         LoadInst *LI = cast<LoadInst>(VL0->getOperand(0));
2823         Builder.SetInsertPoint(LI);
2824         PointerType *PtrTy = PointerType::get(VecTy, LI->getPointerAddressSpace());
2825         Value *Ptr = Builder.CreateBitCast(LI->getOperand(0), PtrTy);
2826         LoadInst *V = Builder.CreateAlignedLoad(Ptr, LI->getAlignment());
2827         E->VectorizedValue = V;
2828         return propagateMetadata(V, E->Scalars);
2829       }
2830       setInsertPointAfterBundle(E->Scalars, VL0);
2831       auto *V = Gather(E->Scalars, VecTy);
2832       E->VectorizedValue = V;
2833       return V;
2834     }
2835     case Instruction::ZExt:
2836     case Instruction::SExt:
2837     case Instruction::FPToUI:
2838     case Instruction::FPToSI:
2839     case Instruction::FPExt:
2840     case Instruction::PtrToInt:
2841     case Instruction::IntToPtr:
2842     case Instruction::SIToFP:
2843     case Instruction::UIToFP:
2844     case Instruction::Trunc:
2845     case Instruction::FPTrunc:
2846     case Instruction::BitCast: {
2847       ValueList INVL;
2848       for (Value *V : E->Scalars)
2849         INVL.push_back(cast<Instruction>(V)->getOperand(0));
2850 
2851       setInsertPointAfterBundle(E->Scalars, VL0);
2852 
2853       Value *InVec = vectorizeTree(INVL);
2854 
2855       if (Value *V = alreadyVectorized(E->Scalars, VL0))
2856         return V;
2857 
2858       CastInst *CI = dyn_cast<CastInst>(VL0);
2859       Value *V = Builder.CreateCast(CI->getOpcode(), InVec, VecTy);
2860       E->VectorizedValue = V;
2861       ++NumVectorInstructions;
2862       return V;
2863     }
2864     case Instruction::FCmp:
2865     case Instruction::ICmp: {
2866       ValueList LHSV, RHSV;
2867       for (Value *V : E->Scalars) {
2868         LHSV.push_back(cast<Instruction>(V)->getOperand(0));
2869         RHSV.push_back(cast<Instruction>(V)->getOperand(1));
2870       }
2871 
2872       setInsertPointAfterBundle(E->Scalars, VL0);
2873 
2874       Value *L = vectorizeTree(LHSV);
2875       Value *R = vectorizeTree(RHSV);
2876 
2877       if (Value *V = alreadyVectorized(E->Scalars, VL0))
2878         return V;
2879 
2880       CmpInst::Predicate P0 = cast<CmpInst>(VL0)->getPredicate();
2881       Value *V;
2882       if (S.Opcode == Instruction::FCmp)
2883         V = Builder.CreateFCmp(P0, L, R);
2884       else
2885         V = Builder.CreateICmp(P0, L, R);
2886 
2887       E->VectorizedValue = V;
2888       propagateIRFlags(E->VectorizedValue, E->Scalars, VL0);
2889       ++NumVectorInstructions;
2890       return V;
2891     }
2892     case Instruction::Select: {
2893       ValueList TrueVec, FalseVec, CondVec;
2894       for (Value *V : E->Scalars) {
2895         CondVec.push_back(cast<Instruction>(V)->getOperand(0));
2896         TrueVec.push_back(cast<Instruction>(V)->getOperand(1));
2897         FalseVec.push_back(cast<Instruction>(V)->getOperand(2));
2898       }
2899 
2900       setInsertPointAfterBundle(E->Scalars, VL0);
2901 
2902       Value *Cond = vectorizeTree(CondVec);
2903       Value *True = vectorizeTree(TrueVec);
2904       Value *False = vectorizeTree(FalseVec);
2905 
2906       if (Value *V = alreadyVectorized(E->Scalars, VL0))
2907         return V;
2908 
2909       Value *V = Builder.CreateSelect(Cond, True, False);
2910       E->VectorizedValue = V;
2911       ++NumVectorInstructions;
2912       return V;
2913     }
2914     case Instruction::Add:
2915     case Instruction::FAdd:
2916     case Instruction::Sub:
2917     case Instruction::FSub:
2918     case Instruction::Mul:
2919     case Instruction::FMul:
2920     case Instruction::UDiv:
2921     case Instruction::SDiv:
2922     case Instruction::FDiv:
2923     case Instruction::URem:
2924     case Instruction::SRem:
2925     case Instruction::FRem:
2926     case Instruction::Shl:
2927     case Instruction::LShr:
2928     case Instruction::AShr:
2929     case Instruction::And:
2930     case Instruction::Or:
2931     case Instruction::Xor: {
2932       ValueList LHSVL, RHSVL;
2933       if (isa<BinaryOperator>(VL0) && VL0->isCommutative())
2934         reorderInputsAccordingToOpcode(S.Opcode, E->Scalars, LHSVL,
2935                                        RHSVL);
2936       else
2937         for (Value *V : E->Scalars) {
2938           auto *I = cast<Instruction>(V);
2939           LHSVL.push_back(I->getOperand(0));
2940           RHSVL.push_back(I->getOperand(1));
2941         }
2942 
2943       setInsertPointAfterBundle(E->Scalars, VL0);
2944 
2945       Value *LHS = vectorizeTree(LHSVL);
2946       Value *RHS = vectorizeTree(RHSVL);
2947 
2948       if (Value *V = alreadyVectorized(E->Scalars, VL0))
2949         return V;
2950 
2951       Value *V = Builder.CreateBinOp(
2952           static_cast<Instruction::BinaryOps>(S.Opcode), LHS, RHS);
2953       E->VectorizedValue = V;
2954       propagateIRFlags(E->VectorizedValue, E->Scalars, VL0);
2955       ++NumVectorInstructions;
2956 
2957       if (Instruction *I = dyn_cast<Instruction>(V))
2958         return propagateMetadata(I, E->Scalars);
2959 
2960       return V;
2961     }
2962     case Instruction::Load: {
2963       // Loads are inserted at the head of the tree because we don't want to
2964       // sink them all the way down past store instructions.
2965       setInsertPointAfterBundle(E->Scalars, VL0);
2966 
2967       LoadInst *LI = cast<LoadInst>(VL0);
2968       Type *ScalarLoadTy = LI->getType();
2969       unsigned AS = LI->getPointerAddressSpace();
2970 
2971       Value *VecPtr = Builder.CreateBitCast(LI->getPointerOperand(),
2972                                             VecTy->getPointerTo(AS));
2973 
2974       // The pointer operand uses an in-tree scalar so we add the new BitCast to
2975       // ExternalUses list to make sure that an extract will be generated in the
2976       // future.
2977       Value *PO = LI->getPointerOperand();
2978       if (getTreeEntry(PO))
2979         ExternalUses.push_back(ExternalUser(PO, cast<User>(VecPtr), 0));
2980 
2981       unsigned Alignment = LI->getAlignment();
2982       LI = Builder.CreateLoad(VecPtr);
2983       if (!Alignment) {
2984         Alignment = DL->getABITypeAlignment(ScalarLoadTy);
2985       }
2986       LI->setAlignment(Alignment);
2987       E->VectorizedValue = LI;
2988       ++NumVectorInstructions;
2989       return propagateMetadata(LI, E->Scalars);
2990     }
2991     case Instruction::Store: {
2992       StoreInst *SI = cast<StoreInst>(VL0);
2993       unsigned Alignment = SI->getAlignment();
2994       unsigned AS = SI->getPointerAddressSpace();
2995 
2996       ValueList ScalarStoreValues;
2997       for (Value *V : E->Scalars)
2998         ScalarStoreValues.push_back(cast<StoreInst>(V)->getValueOperand());
2999 
3000       setInsertPointAfterBundle(E->Scalars, VL0);
3001 
3002       Value *VecValue = vectorizeTree(ScalarStoreValues);
3003       Value *ScalarPtr = SI->getPointerOperand();
3004       Value *VecPtr = Builder.CreateBitCast(ScalarPtr, VecTy->getPointerTo(AS));
3005       StoreInst *S = Builder.CreateStore(VecValue, VecPtr);
3006 
3007       // The pointer operand uses an in-tree scalar, so add the new BitCast to
3008       // ExternalUses to make sure that an extract will be generated in the
3009       // future.
3010       if (getTreeEntry(ScalarPtr))
3011         ExternalUses.push_back(ExternalUser(ScalarPtr, cast<User>(VecPtr), 0));
3012 
3013       if (!Alignment)
3014         Alignment = DL->getABITypeAlignment(SI->getValueOperand()->getType());
3015 
3016       S->setAlignment(Alignment);
3017       E->VectorizedValue = S;
3018       ++NumVectorInstructions;
3019       return propagateMetadata(S, E->Scalars);
3020     }
3021     case Instruction::GetElementPtr: {
3022       setInsertPointAfterBundle(E->Scalars, VL0);
3023 
3024       ValueList Op0VL;
3025       for (Value *V : E->Scalars)
3026         Op0VL.push_back(cast<GetElementPtrInst>(V)->getOperand(0));
3027 
3028       Value *Op0 = vectorizeTree(Op0VL);
3029 
3030       std::vector<Value *> OpVecs;
3031       for (int j = 1, e = cast<GetElementPtrInst>(VL0)->getNumOperands(); j < e;
3032            ++j) {
3033         ValueList OpVL;
3034         for (Value *V : E->Scalars)
3035           OpVL.push_back(cast<GetElementPtrInst>(V)->getOperand(j));
3036 
3037         Value *OpVec = vectorizeTree(OpVL);
3038         OpVecs.push_back(OpVec);
3039       }
3040 
3041       Value *V = Builder.CreateGEP(
3042           cast<GetElementPtrInst>(VL0)->getSourceElementType(), Op0, OpVecs);
3043       E->VectorizedValue = V;
3044       ++NumVectorInstructions;
3045 
3046       if (Instruction *I = dyn_cast<Instruction>(V))
3047         return propagateMetadata(I, E->Scalars);
3048 
3049       return V;
3050     }
3051     case Instruction::Call: {
3052       CallInst *CI = cast<CallInst>(VL0);
3053       setInsertPointAfterBundle(E->Scalars, VL0);
3054       Function *FI;
3055       Intrinsic::ID IID  = Intrinsic::not_intrinsic;
3056       Value *ScalarArg = nullptr;
3057       if (CI && (FI = CI->getCalledFunction())) {
3058         IID = FI->getIntrinsicID();
3059       }
3060       std::vector<Value *> OpVecs;
3061       for (int j = 0, e = CI->getNumArgOperands(); j < e; ++j) {
3062         ValueList OpVL;
3063         // ctlz,cttz and powi are special intrinsics whose second argument is
3064         // a scalar. This argument should not be vectorized.
3065         if (hasVectorInstrinsicScalarOpd(IID, 1) && j == 1) {
3066           CallInst *CEI = cast<CallInst>(VL0);
3067           ScalarArg = CEI->getArgOperand(j);
3068           OpVecs.push_back(CEI->getArgOperand(j));
3069           continue;
3070         }
3071         for (Value *V : E->Scalars) {
3072           CallInst *CEI = cast<CallInst>(V);
3073           OpVL.push_back(CEI->getArgOperand(j));
3074         }
3075 
3076         Value *OpVec = vectorizeTree(OpVL);
3077         DEBUG(dbgs() << "SLP: OpVec[" << j << "]: " << *OpVec << "\n");
3078         OpVecs.push_back(OpVec);
3079       }
3080 
3081       Module *M = F->getParent();
3082       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
3083       Type *Tys[] = { VectorType::get(CI->getType(), E->Scalars.size()) };
3084       Function *CF = Intrinsic::getDeclaration(M, ID, Tys);
3085       SmallVector<OperandBundleDef, 1> OpBundles;
3086       CI->getOperandBundlesAsDefs(OpBundles);
3087       Value *V = Builder.CreateCall(CF, OpVecs, OpBundles);
3088 
3089       // The scalar argument uses an in-tree scalar so we add the new vectorized
3090       // call to ExternalUses list to make sure that an extract will be
3091       // generated in the future.
3092       if (ScalarArg && getTreeEntry(ScalarArg))
3093         ExternalUses.push_back(ExternalUser(ScalarArg, cast<User>(V), 0));
3094 
3095       E->VectorizedValue = V;
3096       propagateIRFlags(E->VectorizedValue, E->Scalars, VL0);
3097       ++NumVectorInstructions;
3098       return V;
3099     }
3100     case Instruction::ShuffleVector: {
3101       ValueList LHSVL, RHSVL;
3102       assert(Instruction::isBinaryOp(S.Opcode) &&
3103              "Invalid Shuffle Vector Operand");
3104       reorderAltShuffleOperands(S.Opcode, E->Scalars, LHSVL, RHSVL);
3105       setInsertPointAfterBundle(E->Scalars, VL0);
3106 
3107       Value *LHS = vectorizeTree(LHSVL);
3108       Value *RHS = vectorizeTree(RHSVL);
3109 
3110       if (Value *V = alreadyVectorized(E->Scalars, VL0))
3111         return V;
3112 
3113       // Create a vector of LHS op1 RHS
3114       Value *V0 = Builder.CreateBinOp(
3115           static_cast<Instruction::BinaryOps>(S.Opcode), LHS, RHS);
3116 
3117       unsigned AltOpcode = getAltOpcode(S.Opcode);
3118       // Create a vector of LHS op2 RHS
3119       Value *V1 = Builder.CreateBinOp(
3120           static_cast<Instruction::BinaryOps>(AltOpcode), LHS, RHS);
3121 
3122       // Create shuffle to take alternate operations from the vector.
3123       // Also, gather up odd and even scalar ops to propagate IR flags to
3124       // each vector operation.
3125       ValueList OddScalars, EvenScalars;
3126       unsigned e = E->Scalars.size();
3127       SmallVector<Constant *, 8> Mask(e);
3128       for (unsigned i = 0; i < e; ++i) {
3129         if (isOdd(i)) {
3130           Mask[i] = Builder.getInt32(e + i);
3131           OddScalars.push_back(E->Scalars[i]);
3132         } else {
3133           Mask[i] = Builder.getInt32(i);
3134           EvenScalars.push_back(E->Scalars[i]);
3135         }
3136       }
3137 
3138       Value *ShuffleMask = ConstantVector::get(Mask);
3139       propagateIRFlags(V0, EvenScalars);
3140       propagateIRFlags(V1, OddScalars);
3141 
3142       Value *V = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
3143       E->VectorizedValue = V;
3144       ++NumVectorInstructions;
3145       if (Instruction *I = dyn_cast<Instruction>(V))
3146         return propagateMetadata(I, E->Scalars);
3147 
3148       return V;
3149     }
3150     default:
3151     llvm_unreachable("unknown inst");
3152   }
3153   return nullptr;
3154 }
3155 
3156 Value *BoUpSLP::vectorizeTree() {
3157   ExtraValueToDebugLocsMap ExternallyUsedValues;
3158   return vectorizeTree(ExternallyUsedValues);
3159 }
3160 
3161 Value *
3162 BoUpSLP::vectorizeTree(ExtraValueToDebugLocsMap &ExternallyUsedValues) {
3163   // All blocks must be scheduled before any instructions are inserted.
3164   for (auto &BSIter : BlocksSchedules) {
3165     scheduleBlock(BSIter.second.get());
3166   }
3167 
3168   Builder.SetInsertPoint(&F->getEntryBlock().front());
3169   auto *VectorRoot = vectorizeTree(&VectorizableTree[0]);
3170 
3171   // If the vectorized tree can be rewritten in a smaller type, we truncate the
3172   // vectorized root. InstCombine will then rewrite the entire expression. We
3173   // sign extend the extracted values below.
3174   auto *ScalarRoot = VectorizableTree[0].Scalars[0];
3175   if (MinBWs.count(ScalarRoot)) {
3176     if (auto *I = dyn_cast<Instruction>(VectorRoot))
3177       Builder.SetInsertPoint(&*++BasicBlock::iterator(I));
3178     auto BundleWidth = VectorizableTree[0].Scalars.size();
3179     auto *MinTy = IntegerType::get(F->getContext(), MinBWs[ScalarRoot].first);
3180     auto *VecTy = VectorType::get(MinTy, BundleWidth);
3181     auto *Trunc = Builder.CreateTrunc(VectorRoot, VecTy);
3182     VectorizableTree[0].VectorizedValue = Trunc;
3183   }
3184 
3185   DEBUG(dbgs() << "SLP: Extracting " << ExternalUses.size() << " values .\n");
3186 
3187   // If necessary, sign-extend or zero-extend ScalarRoot to the larger type
3188   // specified by ScalarType.
3189   auto extend = [&](Value *ScalarRoot, Value *Ex, Type *ScalarType) {
3190     if (!MinBWs.count(ScalarRoot))
3191       return Ex;
3192     if (MinBWs[ScalarRoot].second)
3193       return Builder.CreateSExt(Ex, ScalarType);
3194     return Builder.CreateZExt(Ex, ScalarType);
3195   };
3196 
3197   // Extract all of the elements with the external uses.
3198   for (const auto &ExternalUse : ExternalUses) {
3199     Value *Scalar = ExternalUse.Scalar;
3200     llvm::User *User = ExternalUse.User;
3201 
3202     // Skip users that we already RAUW. This happens when one instruction
3203     // has multiple uses of the same value.
3204     if (User && !is_contained(Scalar->users(), User))
3205       continue;
3206     TreeEntry *E = getTreeEntry(Scalar);
3207     assert(E && "Invalid scalar");
3208     assert(!E->NeedToGather && "Extracting from a gather list");
3209 
3210     Value *Vec = E->VectorizedValue;
3211     assert(Vec && "Can't find vectorizable value");
3212 
3213     Value *Lane = Builder.getInt32(ExternalUse.Lane);
3214     // If User == nullptr, the Scalar is used as extra arg. Generate
3215     // ExtractElement instruction and update the record for this scalar in
3216     // ExternallyUsedValues.
3217     if (!User) {
3218       assert(ExternallyUsedValues.count(Scalar) &&
3219              "Scalar with nullptr as an external user must be registered in "
3220              "ExternallyUsedValues map");
3221       if (auto *VecI = dyn_cast<Instruction>(Vec)) {
3222         Builder.SetInsertPoint(VecI->getParent(),
3223                                std::next(VecI->getIterator()));
3224       } else {
3225         Builder.SetInsertPoint(&F->getEntryBlock().front());
3226       }
3227       Value *Ex = Builder.CreateExtractElement(Vec, Lane);
3228       Ex = extend(ScalarRoot, Ex, Scalar->getType());
3229       CSEBlocks.insert(cast<Instruction>(Scalar)->getParent());
3230       auto &Locs = ExternallyUsedValues[Scalar];
3231       ExternallyUsedValues.insert({Ex, Locs});
3232       ExternallyUsedValues.erase(Scalar);
3233       continue;
3234     }
3235 
3236     // Generate extracts for out-of-tree users.
3237     // Find the insertion point for the extractelement lane.
3238     if (auto *VecI = dyn_cast<Instruction>(Vec)) {
3239       if (PHINode *PH = dyn_cast<PHINode>(User)) {
3240         for (int i = 0, e = PH->getNumIncomingValues(); i != e; ++i) {
3241           if (PH->getIncomingValue(i) == Scalar) {
3242             TerminatorInst *IncomingTerminator =
3243                 PH->getIncomingBlock(i)->getTerminator();
3244             if (isa<CatchSwitchInst>(IncomingTerminator)) {
3245               Builder.SetInsertPoint(VecI->getParent(),
3246                                      std::next(VecI->getIterator()));
3247             } else {
3248               Builder.SetInsertPoint(PH->getIncomingBlock(i)->getTerminator());
3249             }
3250             Value *Ex = Builder.CreateExtractElement(Vec, Lane);
3251             Ex = extend(ScalarRoot, Ex, Scalar->getType());
3252             CSEBlocks.insert(PH->getIncomingBlock(i));
3253             PH->setOperand(i, Ex);
3254           }
3255         }
3256       } else {
3257         Builder.SetInsertPoint(cast<Instruction>(User));
3258         Value *Ex = Builder.CreateExtractElement(Vec, Lane);
3259         Ex = extend(ScalarRoot, Ex, Scalar->getType());
3260         CSEBlocks.insert(cast<Instruction>(User)->getParent());
3261         User->replaceUsesOfWith(Scalar, Ex);
3262      }
3263     } else {
3264       Builder.SetInsertPoint(&F->getEntryBlock().front());
3265       Value *Ex = Builder.CreateExtractElement(Vec, Lane);
3266       Ex = extend(ScalarRoot, Ex, Scalar->getType());
3267       CSEBlocks.insert(&F->getEntryBlock());
3268       User->replaceUsesOfWith(Scalar, Ex);
3269     }
3270 
3271     DEBUG(dbgs() << "SLP: Replaced:" << *User << ".\n");
3272   }
3273 
3274   // For each vectorized value:
3275   for (TreeEntry &EIdx : VectorizableTree) {
3276     TreeEntry *Entry = &EIdx;
3277 
3278     // No need to handle users of gathered values.
3279     if (Entry->NeedToGather)
3280       continue;
3281 
3282     assert(Entry->VectorizedValue && "Can't find vectorizable value");
3283 
3284     // For each lane:
3285     for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
3286       Value *Scalar = Entry->Scalars[Lane];
3287 
3288       Type *Ty = Scalar->getType();
3289       if (!Ty->isVoidTy()) {
3290 #ifndef NDEBUG
3291         for (User *U : Scalar->users()) {
3292           DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
3293 
3294           // It is legal to replace users in the ignorelist by undef.
3295           assert((getTreeEntry(U) || is_contained(UserIgnoreList, U)) &&
3296                  "Replacing out-of-tree value with undef");
3297         }
3298 #endif
3299         Value *Undef = UndefValue::get(Ty);
3300         Scalar->replaceAllUsesWith(Undef);
3301       }
3302       DEBUG(dbgs() << "SLP: \tErasing scalar:" << *Scalar << ".\n");
3303       eraseInstruction(cast<Instruction>(Scalar));
3304     }
3305   }
3306 
3307   Builder.ClearInsertionPoint();
3308 
3309   return VectorizableTree[0].VectorizedValue;
3310 }
3311 
3312 void BoUpSLP::optimizeGatherSequence(Function &F) {
3313   DEBUG(dbgs() << "SLP: Optimizing " << GatherSeq.size()
3314         << " gather sequences instructions.\n");
3315   // LICM InsertElementInst sequences.
3316   for (Instruction *it : GatherSeq) {
3317     InsertElementInst *Insert = dyn_cast<InsertElementInst>(it);
3318 
3319     if (!Insert)
3320       continue;
3321 
3322     // Check if this block is inside a loop.
3323     Loop *L = LI->getLoopFor(Insert->getParent());
3324     if (!L)
3325       continue;
3326 
3327     // Check if it has a preheader.
3328     BasicBlock *PreHeader = L->getLoopPreheader();
3329     if (!PreHeader)
3330       continue;
3331 
3332     // If the vector or the element that we insert into it are
3333     // instructions that are defined in this basic block then we can't
3334     // hoist this instruction.
3335     Instruction *CurrVec = dyn_cast<Instruction>(Insert->getOperand(0));
3336     Instruction *NewElem = dyn_cast<Instruction>(Insert->getOperand(1));
3337     if (CurrVec && L->contains(CurrVec))
3338       continue;
3339     if (NewElem && L->contains(NewElem))
3340       continue;
3341 
3342     // We can hoist this instruction. Move it to the pre-header.
3343     Insert->moveBefore(PreHeader->getTerminator());
3344   }
3345 
3346   // Perform O(N^2) search over the gather sequences and merge identical
3347   // instructions. TODO: We can further optimize this scan if we split the
3348   // instructions into different buckets based on the insert lane.
3349   SmallVector<Instruction *, 16> Visited;
3350   ReversePostOrderTraversal<Function *> RPOT(&F);
3351   for (auto BB : RPOT) {
3352     // Traverse CSEBlocks by RPOT order.
3353     if (!CSEBlocks.count(BB))
3354       continue;
3355 
3356     // For all instructions in blocks containing gather sequences:
3357     for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e;) {
3358       Instruction *In = &*it++;
3359       if (!isa<InsertElementInst>(In) && !isa<ExtractElementInst>(In))
3360         continue;
3361 
3362       // Check if we can replace this instruction with any of the
3363       // visited instructions.
3364       for (Instruction *v : Visited) {
3365         if (In->isIdenticalTo(v) &&
3366             DT->dominates(v->getParent(), In->getParent())) {
3367           In->replaceAllUsesWith(v);
3368           eraseInstruction(In);
3369           In = nullptr;
3370           break;
3371         }
3372       }
3373       if (In) {
3374         assert(!is_contained(Visited, In));
3375         Visited.push_back(In);
3376       }
3377     }
3378   }
3379   CSEBlocks.clear();
3380   GatherSeq.clear();
3381 }
3382 
3383 // Groups the instructions to a bundle (which is then a single scheduling entity)
3384 // and schedules instructions until the bundle gets ready.
3385 bool BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL,
3386                                                  BoUpSLP *SLP, Value *OpValue) {
3387   if (isa<PHINode>(OpValue))
3388     return true;
3389 
3390   // Initialize the instruction bundle.
3391   Instruction *OldScheduleEnd = ScheduleEnd;
3392   ScheduleData *PrevInBundle = nullptr;
3393   ScheduleData *Bundle = nullptr;
3394   bool ReSchedule = false;
3395   DEBUG(dbgs() << "SLP:  bundle: " << *OpValue << "\n");
3396 
3397   // Make sure that the scheduling region contains all
3398   // instructions of the bundle.
3399   for (Value *V : VL) {
3400     if (!extendSchedulingRegion(V, OpValue))
3401       return false;
3402   }
3403 
3404   for (Value *V : VL) {
3405     ScheduleData *BundleMember = getScheduleData(V);
3406     assert(BundleMember &&
3407            "no ScheduleData for bundle member (maybe not in same basic block)");
3408     if (BundleMember->IsScheduled) {
3409       // A bundle member was scheduled as single instruction before and now
3410       // needs to be scheduled as part of the bundle. We just get rid of the
3411       // existing schedule.
3412       DEBUG(dbgs() << "SLP:  reset schedule because " << *BundleMember
3413                    << " was already scheduled\n");
3414       ReSchedule = true;
3415     }
3416     assert(BundleMember->isSchedulingEntity() &&
3417            "bundle member already part of other bundle");
3418     if (PrevInBundle) {
3419       PrevInBundle->NextInBundle = BundleMember;
3420     } else {
3421       Bundle = BundleMember;
3422     }
3423     BundleMember->UnscheduledDepsInBundle = 0;
3424     Bundle->UnscheduledDepsInBundle += BundleMember->UnscheduledDeps;
3425 
3426     // Group the instructions to a bundle.
3427     BundleMember->FirstInBundle = Bundle;
3428     PrevInBundle = BundleMember;
3429   }
3430   if (ScheduleEnd != OldScheduleEnd) {
3431     // The scheduling region got new instructions at the lower end (or it is a
3432     // new region for the first bundle). This makes it necessary to
3433     // recalculate all dependencies.
3434     // It is seldom that this needs to be done a second time after adding the
3435     // initial bundle to the region.
3436     for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
3437       doForAllOpcodes(I, [](ScheduleData *SD) {
3438         SD->clearDependencies();
3439       });
3440     }
3441     ReSchedule = true;
3442   }
3443   if (ReSchedule) {
3444     resetSchedule();
3445     initialFillReadyList(ReadyInsts);
3446   }
3447 
3448   DEBUG(dbgs() << "SLP: try schedule bundle " << *Bundle << " in block "
3449                << BB->getName() << "\n");
3450 
3451   calculateDependencies(Bundle, true, SLP);
3452 
3453   // Now try to schedule the new bundle. As soon as the bundle is "ready" it
3454   // means that there are no cyclic dependencies and we can schedule it.
3455   // Note that's important that we don't "schedule" the bundle yet (see
3456   // cancelScheduling).
3457   while (!Bundle->isReady() && !ReadyInsts.empty()) {
3458 
3459     ScheduleData *pickedSD = ReadyInsts.back();
3460     ReadyInsts.pop_back();
3461 
3462     if (pickedSD->isSchedulingEntity() && pickedSD->isReady()) {
3463       schedule(pickedSD, ReadyInsts);
3464     }
3465   }
3466   if (!Bundle->isReady()) {
3467     cancelScheduling(VL, OpValue);
3468     return false;
3469   }
3470   return true;
3471 }
3472 
3473 void BoUpSLP::BlockScheduling::cancelScheduling(ArrayRef<Value *> VL,
3474                                                 Value *OpValue) {
3475   if (isa<PHINode>(OpValue))
3476     return;
3477 
3478   ScheduleData *Bundle = getScheduleData(OpValue);
3479   DEBUG(dbgs() << "SLP:  cancel scheduling of " << *Bundle << "\n");
3480   assert(!Bundle->IsScheduled &&
3481          "Can't cancel bundle which is already scheduled");
3482   assert(Bundle->isSchedulingEntity() && Bundle->isPartOfBundle() &&
3483          "tried to unbundle something which is not a bundle");
3484 
3485   // Un-bundle: make single instructions out of the bundle.
3486   ScheduleData *BundleMember = Bundle;
3487   while (BundleMember) {
3488     assert(BundleMember->FirstInBundle == Bundle && "corrupt bundle links");
3489     BundleMember->FirstInBundle = BundleMember;
3490     ScheduleData *Next = BundleMember->NextInBundle;
3491     BundleMember->NextInBundle = nullptr;
3492     BundleMember->UnscheduledDepsInBundle = BundleMember->UnscheduledDeps;
3493     if (BundleMember->UnscheduledDepsInBundle == 0) {
3494       ReadyInsts.insert(BundleMember);
3495     }
3496     BundleMember = Next;
3497   }
3498 }
3499 
3500 BoUpSLP::ScheduleData *BoUpSLP::BlockScheduling::allocateScheduleDataChunks() {
3501   // Allocate a new ScheduleData for the instruction.
3502   if (ChunkPos >= ChunkSize) {
3503     ScheduleDataChunks.push_back(llvm::make_unique<ScheduleData[]>(ChunkSize));
3504     ChunkPos = 0;
3505   }
3506   return &(ScheduleDataChunks.back()[ChunkPos++]);
3507 }
3508 
3509 bool BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V,
3510                                                       Value *OpValue) {
3511   if (getScheduleData(V, isOneOf(OpValue, V)))
3512     return true;
3513   Instruction *I = dyn_cast<Instruction>(V);
3514   assert(I && "bundle member must be an instruction");
3515   assert(!isa<PHINode>(I) && "phi nodes don't need to be scheduled");
3516   auto &&CheckSheduleForI = [this, OpValue](Instruction *I) -> bool {
3517     ScheduleData *ISD = getScheduleData(I);
3518     if (!ISD)
3519       return false;
3520     assert(isInSchedulingRegion(ISD) &&
3521            "ScheduleData not in scheduling region");
3522     ScheduleData *SD = allocateScheduleDataChunks();
3523     SD->Inst = I;
3524     SD->init(SchedulingRegionID, OpValue);
3525     ExtraScheduleDataMap[I][OpValue] = SD;
3526     return true;
3527   };
3528   if (CheckSheduleForI(I))
3529     return true;
3530   if (!ScheduleStart) {
3531     // It's the first instruction in the new region.
3532     initScheduleData(I, I->getNextNode(), nullptr, nullptr);
3533     ScheduleStart = I;
3534     ScheduleEnd = I->getNextNode();
3535     if (isOneOf(OpValue, I) != I)
3536       CheckSheduleForI(I);
3537     assert(ScheduleEnd && "tried to vectorize a TerminatorInst?");
3538     DEBUG(dbgs() << "SLP:  initialize schedule region to " << *I << "\n");
3539     return true;
3540   }
3541   // Search up and down at the same time, because we don't know if the new
3542   // instruction is above or below the existing scheduling region.
3543   BasicBlock::reverse_iterator UpIter =
3544       ++ScheduleStart->getIterator().getReverse();
3545   BasicBlock::reverse_iterator UpperEnd = BB->rend();
3546   BasicBlock::iterator DownIter = ScheduleEnd->getIterator();
3547   BasicBlock::iterator LowerEnd = BB->end();
3548   while (true) {
3549     if (++ScheduleRegionSize > ScheduleRegionSizeLimit) {
3550       DEBUG(dbgs() << "SLP:  exceeded schedule region size limit\n");
3551       return false;
3552     }
3553 
3554     if (UpIter != UpperEnd) {
3555       if (&*UpIter == I) {
3556         initScheduleData(I, ScheduleStart, nullptr, FirstLoadStoreInRegion);
3557         ScheduleStart = I;
3558         if (isOneOf(OpValue, I) != I)
3559           CheckSheduleForI(I);
3560         DEBUG(dbgs() << "SLP:  extend schedule region start to " << *I << "\n");
3561         return true;
3562       }
3563       UpIter++;
3564     }
3565     if (DownIter != LowerEnd) {
3566       if (&*DownIter == I) {
3567         initScheduleData(ScheduleEnd, I->getNextNode(), LastLoadStoreInRegion,
3568                          nullptr);
3569         ScheduleEnd = I->getNextNode();
3570         if (isOneOf(OpValue, I) != I)
3571           CheckSheduleForI(I);
3572         assert(ScheduleEnd && "tried to vectorize a TerminatorInst?");
3573         DEBUG(dbgs() << "SLP:  extend schedule region end to " << *I << "\n");
3574         return true;
3575       }
3576       DownIter++;
3577     }
3578     assert((UpIter != UpperEnd || DownIter != LowerEnd) &&
3579            "instruction not found in block");
3580   }
3581   return true;
3582 }
3583 
3584 void BoUpSLP::BlockScheduling::initScheduleData(Instruction *FromI,
3585                                                 Instruction *ToI,
3586                                                 ScheduleData *PrevLoadStore,
3587                                                 ScheduleData *NextLoadStore) {
3588   ScheduleData *CurrentLoadStore = PrevLoadStore;
3589   for (Instruction *I = FromI; I != ToI; I = I->getNextNode()) {
3590     ScheduleData *SD = ScheduleDataMap[I];
3591     if (!SD) {
3592       SD = allocateScheduleDataChunks();
3593       ScheduleDataMap[I] = SD;
3594       SD->Inst = I;
3595     }
3596     assert(!isInSchedulingRegion(SD) &&
3597            "new ScheduleData already in scheduling region");
3598     SD->init(SchedulingRegionID, I);
3599 
3600     if (I->mayReadOrWriteMemory() &&
3601         (!isa<IntrinsicInst>(I) ||
3602          cast<IntrinsicInst>(I)->getIntrinsicID() != Intrinsic::sideeffect)) {
3603       // Update the linked list of memory accessing instructions.
3604       if (CurrentLoadStore) {
3605         CurrentLoadStore->NextLoadStore = SD;
3606       } else {
3607         FirstLoadStoreInRegion = SD;
3608       }
3609       CurrentLoadStore = SD;
3610     }
3611   }
3612   if (NextLoadStore) {
3613     if (CurrentLoadStore)
3614       CurrentLoadStore->NextLoadStore = NextLoadStore;
3615   } else {
3616     LastLoadStoreInRegion = CurrentLoadStore;
3617   }
3618 }
3619 
3620 void BoUpSLP::BlockScheduling::calculateDependencies(ScheduleData *SD,
3621                                                      bool InsertInReadyList,
3622                                                      BoUpSLP *SLP) {
3623   assert(SD->isSchedulingEntity());
3624 
3625   SmallVector<ScheduleData *, 10> WorkList;
3626   WorkList.push_back(SD);
3627 
3628   while (!WorkList.empty()) {
3629     ScheduleData *SD = WorkList.back();
3630     WorkList.pop_back();
3631 
3632     ScheduleData *BundleMember = SD;
3633     while (BundleMember) {
3634       assert(isInSchedulingRegion(BundleMember));
3635       if (!BundleMember->hasValidDependencies()) {
3636 
3637         DEBUG(dbgs() << "SLP:       update deps of " << *BundleMember << "\n");
3638         BundleMember->Dependencies = 0;
3639         BundleMember->resetUnscheduledDeps();
3640 
3641         // Handle def-use chain dependencies.
3642         if (BundleMember->OpValue != BundleMember->Inst) {
3643           ScheduleData *UseSD = getScheduleData(BundleMember->Inst);
3644           if (UseSD && isInSchedulingRegion(UseSD->FirstInBundle)) {
3645             BundleMember->Dependencies++;
3646             ScheduleData *DestBundle = UseSD->FirstInBundle;
3647             if (!DestBundle->IsScheduled)
3648               BundleMember->incrementUnscheduledDeps(1);
3649             if (!DestBundle->hasValidDependencies())
3650               WorkList.push_back(DestBundle);
3651           }
3652         } else {
3653           for (User *U : BundleMember->Inst->users()) {
3654             if (isa<Instruction>(U)) {
3655               ScheduleData *UseSD = getScheduleData(U);
3656               if (UseSD && isInSchedulingRegion(UseSD->FirstInBundle)) {
3657                 BundleMember->Dependencies++;
3658                 ScheduleData *DestBundle = UseSD->FirstInBundle;
3659                 if (!DestBundle->IsScheduled)
3660                   BundleMember->incrementUnscheduledDeps(1);
3661                 if (!DestBundle->hasValidDependencies())
3662                   WorkList.push_back(DestBundle);
3663               }
3664             } else {
3665               // I'm not sure if this can ever happen. But we need to be safe.
3666               // This lets the instruction/bundle never be scheduled and
3667               // eventually disable vectorization.
3668               BundleMember->Dependencies++;
3669               BundleMember->incrementUnscheduledDeps(1);
3670             }
3671           }
3672         }
3673 
3674         // Handle the memory dependencies.
3675         ScheduleData *DepDest = BundleMember->NextLoadStore;
3676         if (DepDest) {
3677           Instruction *SrcInst = BundleMember->Inst;
3678           MemoryLocation SrcLoc = getLocation(SrcInst, SLP->AA);
3679           bool SrcMayWrite = BundleMember->Inst->mayWriteToMemory();
3680           unsigned numAliased = 0;
3681           unsigned DistToSrc = 1;
3682 
3683           while (DepDest) {
3684             assert(isInSchedulingRegion(DepDest));
3685 
3686             // We have two limits to reduce the complexity:
3687             // 1) AliasedCheckLimit: It's a small limit to reduce calls to
3688             //    SLP->isAliased (which is the expensive part in this loop).
3689             // 2) MaxMemDepDistance: It's for very large blocks and it aborts
3690             //    the whole loop (even if the loop is fast, it's quadratic).
3691             //    It's important for the loop break condition (see below) to
3692             //    check this limit even between two read-only instructions.
3693             if (DistToSrc >= MaxMemDepDistance ||
3694                     ((SrcMayWrite || DepDest->Inst->mayWriteToMemory()) &&
3695                      (numAliased >= AliasedCheckLimit ||
3696                       SLP->isAliased(SrcLoc, SrcInst, DepDest->Inst)))) {
3697 
3698               // We increment the counter only if the locations are aliased
3699               // (instead of counting all alias checks). This gives a better
3700               // balance between reduced runtime and accurate dependencies.
3701               numAliased++;
3702 
3703               DepDest->MemoryDependencies.push_back(BundleMember);
3704               BundleMember->Dependencies++;
3705               ScheduleData *DestBundle = DepDest->FirstInBundle;
3706               if (!DestBundle->IsScheduled) {
3707                 BundleMember->incrementUnscheduledDeps(1);
3708               }
3709               if (!DestBundle->hasValidDependencies()) {
3710                 WorkList.push_back(DestBundle);
3711               }
3712             }
3713             DepDest = DepDest->NextLoadStore;
3714 
3715             // Example, explaining the loop break condition: Let's assume our
3716             // starting instruction is i0 and MaxMemDepDistance = 3.
3717             //
3718             //                      +--------v--v--v
3719             //             i0,i1,i2,i3,i4,i5,i6,i7,i8
3720             //             +--------^--^--^
3721             //
3722             // MaxMemDepDistance let us stop alias-checking at i3 and we add
3723             // dependencies from i0 to i3,i4,.. (even if they are not aliased).
3724             // Previously we already added dependencies from i3 to i6,i7,i8
3725             // (because of MaxMemDepDistance). As we added a dependency from
3726             // i0 to i3, we have transitive dependencies from i0 to i6,i7,i8
3727             // and we can abort this loop at i6.
3728             if (DistToSrc >= 2 * MaxMemDepDistance)
3729                 break;
3730             DistToSrc++;
3731           }
3732         }
3733       }
3734       BundleMember = BundleMember->NextInBundle;
3735     }
3736     if (InsertInReadyList && SD->isReady()) {
3737       ReadyInsts.push_back(SD);
3738       DEBUG(dbgs() << "SLP:     gets ready on update: " << *SD->Inst << "\n");
3739     }
3740   }
3741 }
3742 
3743 void BoUpSLP::BlockScheduling::resetSchedule() {
3744   assert(ScheduleStart &&
3745          "tried to reset schedule on block which has not been scheduled");
3746   for (Instruction *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
3747     doForAllOpcodes(I, [&](ScheduleData *SD) {
3748       assert(isInSchedulingRegion(SD) &&
3749              "ScheduleData not in scheduling region");
3750       SD->IsScheduled = false;
3751       SD->resetUnscheduledDeps();
3752     });
3753   }
3754   ReadyInsts.clear();
3755 }
3756 
3757 void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
3758   if (!BS->ScheduleStart)
3759     return;
3760 
3761   DEBUG(dbgs() << "SLP: schedule block " << BS->BB->getName() << "\n");
3762 
3763   BS->resetSchedule();
3764 
3765   // For the real scheduling we use a more sophisticated ready-list: it is
3766   // sorted by the original instruction location. This lets the final schedule
3767   // be as  close as possible to the original instruction order.
3768   struct ScheduleDataCompare {
3769     bool operator()(ScheduleData *SD1, ScheduleData *SD2) const {
3770       return SD2->SchedulingPriority < SD1->SchedulingPriority;
3771     }
3772   };
3773   std::set<ScheduleData *, ScheduleDataCompare> ReadyInsts;
3774 
3775   // Ensure that all dependency data is updated and fill the ready-list with
3776   // initial instructions.
3777   int Idx = 0;
3778   int NumToSchedule = 0;
3779   for (auto *I = BS->ScheduleStart; I != BS->ScheduleEnd;
3780        I = I->getNextNode()) {
3781     BS->doForAllOpcodes(I, [this, &Idx, &NumToSchedule, BS](ScheduleData *SD) {
3782       assert(SD->isPartOfBundle() ==
3783                  (getTreeEntry(SD->Inst) != nullptr) &&
3784              "scheduler and vectorizer bundle mismatch");
3785       SD->FirstInBundle->SchedulingPriority = Idx++;
3786       if (SD->isSchedulingEntity()) {
3787         BS->calculateDependencies(SD, false, this);
3788         NumToSchedule++;
3789       }
3790     });
3791   }
3792   BS->initialFillReadyList(ReadyInsts);
3793 
3794   Instruction *LastScheduledInst = BS->ScheduleEnd;
3795 
3796   // Do the "real" scheduling.
3797   while (!ReadyInsts.empty()) {
3798     ScheduleData *picked = *ReadyInsts.begin();
3799     ReadyInsts.erase(ReadyInsts.begin());
3800 
3801     // Move the scheduled instruction(s) to their dedicated places, if not
3802     // there yet.
3803     ScheduleData *BundleMember = picked;
3804     while (BundleMember) {
3805       Instruction *pickedInst = BundleMember->Inst;
3806       if (LastScheduledInst->getNextNode() != pickedInst) {
3807         BS->BB->getInstList().remove(pickedInst);
3808         BS->BB->getInstList().insert(LastScheduledInst->getIterator(),
3809                                      pickedInst);
3810       }
3811       LastScheduledInst = pickedInst;
3812       BundleMember = BundleMember->NextInBundle;
3813     }
3814 
3815     BS->schedule(picked, ReadyInsts);
3816     NumToSchedule--;
3817   }
3818   assert(NumToSchedule == 0 && "could not schedule all instructions");
3819 
3820   // Avoid duplicate scheduling of the block.
3821   BS->ScheduleStart = nullptr;
3822 }
3823 
3824 unsigned BoUpSLP::getVectorElementSize(Value *V) {
3825   // If V is a store, just return the width of the stored value without
3826   // traversing the expression tree. This is the common case.
3827   if (auto *Store = dyn_cast<StoreInst>(V))
3828     return DL->getTypeSizeInBits(Store->getValueOperand()->getType());
3829 
3830   // If V is not a store, we can traverse the expression tree to find loads
3831   // that feed it. The type of the loaded value may indicate a more suitable
3832   // width than V's type. We want to base the vector element size on the width
3833   // of memory operations where possible.
3834   SmallVector<Instruction *, 16> Worklist;
3835   SmallPtrSet<Instruction *, 16> Visited;
3836   if (auto *I = dyn_cast<Instruction>(V))
3837     Worklist.push_back(I);
3838 
3839   // Traverse the expression tree in bottom-up order looking for loads. If we
3840   // encounter an instruciton we don't yet handle, we give up.
3841   auto MaxWidth = 0u;
3842   auto FoundUnknownInst = false;
3843   while (!Worklist.empty() && !FoundUnknownInst) {
3844     auto *I = Worklist.pop_back_val();
3845     Visited.insert(I);
3846 
3847     // We should only be looking at scalar instructions here. If the current
3848     // instruction has a vector type, give up.
3849     auto *Ty = I->getType();
3850     if (isa<VectorType>(Ty))
3851       FoundUnknownInst = true;
3852 
3853     // If the current instruction is a load, update MaxWidth to reflect the
3854     // width of the loaded value.
3855     else if (isa<LoadInst>(I))
3856       MaxWidth = std::max<unsigned>(MaxWidth, DL->getTypeSizeInBits(Ty));
3857 
3858     // Otherwise, we need to visit the operands of the instruction. We only
3859     // handle the interesting cases from buildTree here. If an operand is an
3860     // instruction we haven't yet visited, we add it to the worklist.
3861     else if (isa<PHINode>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
3862              isa<CmpInst>(I) || isa<SelectInst>(I) || isa<BinaryOperator>(I)) {
3863       for (Use &U : I->operands())
3864         if (auto *J = dyn_cast<Instruction>(U.get()))
3865           if (!Visited.count(J))
3866             Worklist.push_back(J);
3867     }
3868 
3869     // If we don't yet handle the instruction, give up.
3870     else
3871       FoundUnknownInst = true;
3872   }
3873 
3874   // If we didn't encounter a memory access in the expression tree, or if we
3875   // gave up for some reason, just return the width of V.
3876   if (!MaxWidth || FoundUnknownInst)
3877     return DL->getTypeSizeInBits(V->getType());
3878 
3879   // Otherwise, return the maximum width we found.
3880   return MaxWidth;
3881 }
3882 
3883 // Determine if a value V in a vectorizable expression Expr can be demoted to a
3884 // smaller type with a truncation. We collect the values that will be demoted
3885 // in ToDemote and additional roots that require investigating in Roots.
3886 static bool collectValuesToDemote(Value *V, SmallPtrSetImpl<Value *> &Expr,
3887                                   SmallVectorImpl<Value *> &ToDemote,
3888                                   SmallVectorImpl<Value *> &Roots) {
3889   // We can always demote constants.
3890   if (isa<Constant>(V)) {
3891     ToDemote.push_back(V);
3892     return true;
3893   }
3894 
3895   // If the value is not an instruction in the expression with only one use, it
3896   // cannot be demoted.
3897   auto *I = dyn_cast<Instruction>(V);
3898   if (!I || !I->hasOneUse() || !Expr.count(I))
3899     return false;
3900 
3901   switch (I->getOpcode()) {
3902 
3903   // We can always demote truncations and extensions. Since truncations can
3904   // seed additional demotion, we save the truncated value.
3905   case Instruction::Trunc:
3906     Roots.push_back(I->getOperand(0));
3907     break;
3908   case Instruction::ZExt:
3909   case Instruction::SExt:
3910     break;
3911 
3912   // We can demote certain binary operations if we can demote both of their
3913   // operands.
3914   case Instruction::Add:
3915   case Instruction::Sub:
3916   case Instruction::Mul:
3917   case Instruction::And:
3918   case Instruction::Or:
3919   case Instruction::Xor:
3920     if (!collectValuesToDemote(I->getOperand(0), Expr, ToDemote, Roots) ||
3921         !collectValuesToDemote(I->getOperand(1), Expr, ToDemote, Roots))
3922       return false;
3923     break;
3924 
3925   // We can demote selects if we can demote their true and false values.
3926   case Instruction::Select: {
3927     SelectInst *SI = cast<SelectInst>(I);
3928     if (!collectValuesToDemote(SI->getTrueValue(), Expr, ToDemote, Roots) ||
3929         !collectValuesToDemote(SI->getFalseValue(), Expr, ToDemote, Roots))
3930       return false;
3931     break;
3932   }
3933 
3934   // We can demote phis if we can demote all their incoming operands. Note that
3935   // we don't need to worry about cycles since we ensure single use above.
3936   case Instruction::PHI: {
3937     PHINode *PN = cast<PHINode>(I);
3938     for (Value *IncValue : PN->incoming_values())
3939       if (!collectValuesToDemote(IncValue, Expr, ToDemote, Roots))
3940         return false;
3941     break;
3942   }
3943 
3944   // Otherwise, conservatively give up.
3945   default:
3946     return false;
3947   }
3948 
3949   // Record the value that we can demote.
3950   ToDemote.push_back(V);
3951   return true;
3952 }
3953 
3954 void BoUpSLP::computeMinimumValueSizes() {
3955   // If there are no external uses, the expression tree must be rooted by a
3956   // store. We can't demote in-memory values, so there is nothing to do here.
3957   if (ExternalUses.empty())
3958     return;
3959 
3960   // We only attempt to truncate integer expressions.
3961   auto &TreeRoot = VectorizableTree[0].Scalars;
3962   auto *TreeRootIT = dyn_cast<IntegerType>(TreeRoot[0]->getType());
3963   if (!TreeRootIT)
3964     return;
3965 
3966   // If the expression is not rooted by a store, these roots should have
3967   // external uses. We will rely on InstCombine to rewrite the expression in
3968   // the narrower type. However, InstCombine only rewrites single-use values.
3969   // This means that if a tree entry other than a root is used externally, it
3970   // must have multiple uses and InstCombine will not rewrite it. The code
3971   // below ensures that only the roots are used externally.
3972   SmallPtrSet<Value *, 32> Expr(TreeRoot.begin(), TreeRoot.end());
3973   for (auto &EU : ExternalUses)
3974     if (!Expr.erase(EU.Scalar))
3975       return;
3976   if (!Expr.empty())
3977     return;
3978 
3979   // Collect the scalar values of the vectorizable expression. We will use this
3980   // context to determine which values can be demoted. If we see a truncation,
3981   // we mark it as seeding another demotion.
3982   for (auto &Entry : VectorizableTree)
3983     Expr.insert(Entry.Scalars.begin(), Entry.Scalars.end());
3984 
3985   // Ensure the roots of the vectorizable tree don't form a cycle. They must
3986   // have a single external user that is not in the vectorizable tree.
3987   for (auto *Root : TreeRoot)
3988     if (!Root->hasOneUse() || Expr.count(*Root->user_begin()))
3989       return;
3990 
3991   // Conservatively determine if we can actually truncate the roots of the
3992   // expression. Collect the values that can be demoted in ToDemote and
3993   // additional roots that require investigating in Roots.
3994   SmallVector<Value *, 32> ToDemote;
3995   SmallVector<Value *, 4> Roots;
3996   for (auto *Root : TreeRoot)
3997     if (!collectValuesToDemote(Root, Expr, ToDemote, Roots))
3998       return;
3999 
4000   // The maximum bit width required to represent all the values that can be
4001   // demoted without loss of precision. It would be safe to truncate the roots
4002   // of the expression to this width.
4003   auto MaxBitWidth = 8u;
4004 
4005   // We first check if all the bits of the roots are demanded. If they're not,
4006   // we can truncate the roots to this narrower type.
4007   for (auto *Root : TreeRoot) {
4008     auto Mask = DB->getDemandedBits(cast<Instruction>(Root));
4009     MaxBitWidth = std::max<unsigned>(
4010         Mask.getBitWidth() - Mask.countLeadingZeros(), MaxBitWidth);
4011   }
4012 
4013   // True if the roots can be zero-extended back to their original type, rather
4014   // than sign-extended. We know that if the leading bits are not demanded, we
4015   // can safely zero-extend. So we initialize IsKnownPositive to True.
4016   bool IsKnownPositive = true;
4017 
4018   // If all the bits of the roots are demanded, we can try a little harder to
4019   // compute a narrower type. This can happen, for example, if the roots are
4020   // getelementptr indices. InstCombine promotes these indices to the pointer
4021   // width. Thus, all their bits are technically demanded even though the
4022   // address computation might be vectorized in a smaller type.
4023   //
4024   // We start by looking at each entry that can be demoted. We compute the
4025   // maximum bit width required to store the scalar by using ValueTracking to
4026   // compute the number of high-order bits we can truncate.
4027   if (MaxBitWidth == DL->getTypeSizeInBits(TreeRoot[0]->getType())) {
4028     MaxBitWidth = 8u;
4029 
4030     // Determine if the sign bit of all the roots is known to be zero. If not,
4031     // IsKnownPositive is set to False.
4032     IsKnownPositive = llvm::all_of(TreeRoot, [&](Value *R) {
4033       KnownBits Known = computeKnownBits(R, *DL);
4034       return Known.isNonNegative();
4035     });
4036 
4037     // Determine the maximum number of bits required to store the scalar
4038     // values.
4039     for (auto *Scalar : ToDemote) {
4040       auto NumSignBits = ComputeNumSignBits(Scalar, *DL, 0, AC, nullptr, DT);
4041       auto NumTypeBits = DL->getTypeSizeInBits(Scalar->getType());
4042       MaxBitWidth = std::max<unsigned>(NumTypeBits - NumSignBits, MaxBitWidth);
4043     }
4044 
4045     // If we can't prove that the sign bit is zero, we must add one to the
4046     // maximum bit width to account for the unknown sign bit. This preserves
4047     // the existing sign bit so we can safely sign-extend the root back to the
4048     // original type. Otherwise, if we know the sign bit is zero, we will
4049     // zero-extend the root instead.
4050     //
4051     // FIXME: This is somewhat suboptimal, as there will be cases where adding
4052     //        one to the maximum bit width will yield a larger-than-necessary
4053     //        type. In general, we need to add an extra bit only if we can't
4054     //        prove that the upper bit of the original type is equal to the
4055     //        upper bit of the proposed smaller type. If these two bits are the
4056     //        same (either zero or one) we know that sign-extending from the
4057     //        smaller type will result in the same value. Here, since we can't
4058     //        yet prove this, we are just making the proposed smaller type
4059     //        larger to ensure correctness.
4060     if (!IsKnownPositive)
4061       ++MaxBitWidth;
4062   }
4063 
4064   // Round MaxBitWidth up to the next power-of-two.
4065   if (!isPowerOf2_64(MaxBitWidth))
4066     MaxBitWidth = NextPowerOf2(MaxBitWidth);
4067 
4068   // If the maximum bit width we compute is less than the with of the roots'
4069   // type, we can proceed with the narrowing. Otherwise, do nothing.
4070   if (MaxBitWidth >= TreeRootIT->getBitWidth())
4071     return;
4072 
4073   // If we can truncate the root, we must collect additional values that might
4074   // be demoted as a result. That is, those seeded by truncations we will
4075   // modify.
4076   while (!Roots.empty())
4077     collectValuesToDemote(Roots.pop_back_val(), Expr, ToDemote, Roots);
4078 
4079   // Finally, map the values we can demote to the maximum bit with we computed.
4080   for (auto *Scalar : ToDemote)
4081     MinBWs[Scalar] = std::make_pair(MaxBitWidth, !IsKnownPositive);
4082 }
4083 
4084 namespace {
4085 
4086 /// The SLPVectorizer Pass.
4087 struct SLPVectorizer : public FunctionPass {
4088   SLPVectorizerPass Impl;
4089 
4090   /// Pass identification, replacement for typeid
4091   static char ID;
4092 
4093   explicit SLPVectorizer() : FunctionPass(ID) {
4094     initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
4095   }
4096 
4097   bool doInitialization(Module &M) override {
4098     return false;
4099   }
4100 
4101   bool runOnFunction(Function &F) override {
4102     if (skipFunction(F))
4103       return false;
4104 
4105     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
4106     auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
4107     auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
4108     auto *TLI = TLIP ? &TLIP->getTLI() : nullptr;
4109     auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
4110     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
4111     auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
4112     auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
4113     auto *DB = &getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
4114     auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
4115 
4116     return Impl.runImpl(F, SE, TTI, TLI, AA, LI, DT, AC, DB, ORE);
4117   }
4118 
4119   void getAnalysisUsage(AnalysisUsage &AU) const override {
4120     FunctionPass::getAnalysisUsage(AU);
4121     AU.addRequired<AssumptionCacheTracker>();
4122     AU.addRequired<ScalarEvolutionWrapperPass>();
4123     AU.addRequired<AAResultsWrapperPass>();
4124     AU.addRequired<TargetTransformInfoWrapperPass>();
4125     AU.addRequired<LoopInfoWrapperPass>();
4126     AU.addRequired<DominatorTreeWrapperPass>();
4127     AU.addRequired<DemandedBitsWrapperPass>();
4128     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
4129     AU.addPreserved<LoopInfoWrapperPass>();
4130     AU.addPreserved<DominatorTreeWrapperPass>();
4131     AU.addPreserved<AAResultsWrapperPass>();
4132     AU.addPreserved<GlobalsAAWrapperPass>();
4133     AU.setPreservesCFG();
4134   }
4135 };
4136 
4137 } // end anonymous namespace
4138 
4139 PreservedAnalyses SLPVectorizerPass::run(Function &F, FunctionAnalysisManager &AM) {
4140   auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
4141   auto *TTI = &AM.getResult<TargetIRAnalysis>(F);
4142   auto *TLI = AM.getCachedResult<TargetLibraryAnalysis>(F);
4143   auto *AA = &AM.getResult<AAManager>(F);
4144   auto *LI = &AM.getResult<LoopAnalysis>(F);
4145   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
4146   auto *AC = &AM.getResult<AssumptionAnalysis>(F);
4147   auto *DB = &AM.getResult<DemandedBitsAnalysis>(F);
4148   auto *ORE = &AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
4149 
4150   bool Changed = runImpl(F, SE, TTI, TLI, AA, LI, DT, AC, DB, ORE);
4151   if (!Changed)
4152     return PreservedAnalyses::all();
4153 
4154   PreservedAnalyses PA;
4155   PA.preserveSet<CFGAnalyses>();
4156   PA.preserve<AAManager>();
4157   PA.preserve<GlobalsAA>();
4158   return PA;
4159 }
4160 
4161 bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
4162                                 TargetTransformInfo *TTI_,
4163                                 TargetLibraryInfo *TLI_, AliasAnalysis *AA_,
4164                                 LoopInfo *LI_, DominatorTree *DT_,
4165                                 AssumptionCache *AC_, DemandedBits *DB_,
4166                                 OptimizationRemarkEmitter *ORE_) {
4167   SE = SE_;
4168   TTI = TTI_;
4169   TLI = TLI_;
4170   AA = AA_;
4171   LI = LI_;
4172   DT = DT_;
4173   AC = AC_;
4174   DB = DB_;
4175   DL = &F.getParent()->getDataLayout();
4176 
4177   Stores.clear();
4178   GEPs.clear();
4179   bool Changed = false;
4180 
4181   // If the target claims to have no vector registers don't attempt
4182   // vectorization.
4183   if (!TTI->getNumberOfRegisters(true))
4184     return false;
4185 
4186   // Don't vectorize when the attribute NoImplicitFloat is used.
4187   if (F.hasFnAttribute(Attribute::NoImplicitFloat))
4188     return false;
4189 
4190   DEBUG(dbgs() << "SLP: Analyzing blocks in " << F.getName() << ".\n");
4191 
4192   // Use the bottom up slp vectorizer to construct chains that start with
4193   // store instructions.
4194   BoUpSLP R(&F, SE, TTI, TLI, AA, LI, DT, AC, DB, DL, ORE_);
4195 
4196   // A general note: the vectorizer must use BoUpSLP::eraseInstruction() to
4197   // delete instructions.
4198 
4199   // Scan the blocks in the function in post order.
4200   for (auto BB : post_order(&F.getEntryBlock())) {
4201     collectSeedInstructions(BB);
4202 
4203     // Vectorize trees that end at stores.
4204     if (!Stores.empty()) {
4205       DEBUG(dbgs() << "SLP: Found stores for " << Stores.size()
4206                    << " underlying objects.\n");
4207       Changed |= vectorizeStoreChains(R);
4208     }
4209 
4210     // Vectorize trees that end at reductions.
4211     Changed |= vectorizeChainsInBlock(BB, R);
4212 
4213     // Vectorize the index computations of getelementptr instructions. This
4214     // is primarily intended to catch gather-like idioms ending at
4215     // non-consecutive loads.
4216     if (!GEPs.empty()) {
4217       DEBUG(dbgs() << "SLP: Found GEPs for " << GEPs.size()
4218                    << " underlying objects.\n");
4219       Changed |= vectorizeGEPIndices(BB, R);
4220     }
4221   }
4222 
4223   if (Changed) {
4224     R.optimizeGatherSequence(F);
4225     DEBUG(dbgs() << "SLP: vectorized \"" << F.getName() << "\"\n");
4226     DEBUG(verifyFunction(F));
4227   }
4228   return Changed;
4229 }
4230 
4231 /// \brief Check that the Values in the slice in VL array are still existent in
4232 /// the WeakTrackingVH array.
4233 /// Vectorization of part of the VL array may cause later values in the VL array
4234 /// to become invalid. We track when this has happened in the WeakTrackingVH
4235 /// array.
4236 static bool hasValueBeenRAUWed(ArrayRef<Value *> VL,
4237                                ArrayRef<WeakTrackingVH> VH, unsigned SliceBegin,
4238                                unsigned SliceSize) {
4239   VL = VL.slice(SliceBegin, SliceSize);
4240   VH = VH.slice(SliceBegin, SliceSize);
4241   return !std::equal(VL.begin(), VL.end(), VH.begin());
4242 }
4243 
4244 bool SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
4245                                             unsigned VecRegSize) {
4246   unsigned ChainLen = Chain.size();
4247   DEBUG(dbgs() << "SLP: Analyzing a store chain of length " << ChainLen
4248         << "\n");
4249   unsigned Sz = R.getVectorElementSize(Chain[0]);
4250   unsigned VF = VecRegSize / Sz;
4251 
4252   if (!isPowerOf2_32(Sz) || VF < 2)
4253     return false;
4254 
4255   // Keep track of values that were deleted by vectorizing in the loop below.
4256   SmallVector<WeakTrackingVH, 8> TrackValues(Chain.begin(), Chain.end());
4257 
4258   bool Changed = false;
4259   // Look for profitable vectorizable trees at all offsets, starting at zero.
4260   for (unsigned i = 0, e = ChainLen; i < e; ++i) {
4261     if (i + VF > e)
4262       break;
4263 
4264     // Check that a previous iteration of this loop did not delete the Value.
4265     if (hasValueBeenRAUWed(Chain, TrackValues, i, VF))
4266       continue;
4267 
4268     DEBUG(dbgs() << "SLP: Analyzing " << VF << " stores at offset " << i
4269           << "\n");
4270     ArrayRef<Value *> Operands = Chain.slice(i, VF);
4271 
4272     R.buildTree(Operands);
4273     if (R.isTreeTinyAndNotFullyVectorizable())
4274       continue;
4275 
4276     R.computeMinimumValueSizes();
4277 
4278     int Cost = R.getTreeCost();
4279 
4280     DEBUG(dbgs() << "SLP: Found cost=" << Cost << " for VF=" << VF << "\n");
4281     if (Cost < -SLPCostThreshold) {
4282       DEBUG(dbgs() << "SLP: Decided to vectorize cost=" << Cost << "\n");
4283 
4284       using namespace ore;
4285 
4286       R.getORE()->emit(OptimizationRemark(SV_NAME, "StoresVectorized",
4287                                           cast<StoreInst>(Chain[i]))
4288                        << "Stores SLP vectorized with cost " << NV("Cost", Cost)
4289                        << " and with tree size "
4290                        << NV("TreeSize", R.getTreeSize()));
4291 
4292       R.vectorizeTree();
4293 
4294       // Move to the next bundle.
4295       i += VF - 1;
4296       Changed = true;
4297     }
4298   }
4299 
4300   return Changed;
4301 }
4302 
4303 bool SLPVectorizerPass::vectorizeStores(ArrayRef<StoreInst *> Stores,
4304                                         BoUpSLP &R) {
4305   SetVector<StoreInst *> Heads;
4306   SmallDenseSet<StoreInst *> Tails;
4307   SmallDenseMap<StoreInst *, StoreInst *> ConsecutiveChain;
4308 
4309   // We may run into multiple chains that merge into a single chain. We mark the
4310   // stores that we vectorized so that we don't visit the same store twice.
4311   BoUpSLP::ValueSet VectorizedStores;
4312   bool Changed = false;
4313 
4314   // Do a quadratic search on all of the given stores in reverse order and find
4315   // all of the pairs of stores that follow each other.
4316   SmallVector<unsigned, 16> IndexQueue;
4317   unsigned E = Stores.size();
4318   IndexQueue.resize(E - 1);
4319   for (unsigned I = E; I > 0; --I) {
4320     unsigned Idx = I - 1;
4321     // If a store has multiple consecutive store candidates, search Stores
4322     // array according to the sequence: Idx-1, Idx+1, Idx-2, Idx+2, ...
4323     // This is because usually pairing with immediate succeeding or preceding
4324     // candidate create the best chance to find slp vectorization opportunity.
4325     unsigned Offset = 1;
4326     unsigned Cnt = 0;
4327     for (unsigned J = 0; J < E - 1; ++J, ++Offset) {
4328       if (Idx >= Offset) {
4329         IndexQueue[Cnt] = Idx - Offset;
4330         ++Cnt;
4331       }
4332       if (Idx + Offset < E) {
4333         IndexQueue[Cnt] = Idx + Offset;
4334         ++Cnt;
4335       }
4336     }
4337 
4338     for (auto K : IndexQueue) {
4339       if (isConsecutiveAccess(Stores[K], Stores[Idx], *DL, *SE)) {
4340         Tails.insert(Stores[Idx]);
4341         Heads.insert(Stores[K]);
4342         ConsecutiveChain[Stores[K]] = Stores[Idx];
4343         break;
4344       }
4345     }
4346   }
4347 
4348   // For stores that start but don't end a link in the chain:
4349   for (auto *SI : llvm::reverse(Heads)) {
4350     if (Tails.count(SI))
4351       continue;
4352 
4353     // We found a store instr that starts a chain. Now follow the chain and try
4354     // to vectorize it.
4355     BoUpSLP::ValueList Operands;
4356     StoreInst *I = SI;
4357     // Collect the chain into a list.
4358     while ((Tails.count(I) || Heads.count(I)) && !VectorizedStores.count(I)) {
4359       Operands.push_back(I);
4360       // Move to the next value in the chain.
4361       I = ConsecutiveChain[I];
4362     }
4363 
4364     // FIXME: Is division-by-2 the correct step? Should we assert that the
4365     // register size is a power-of-2?
4366     for (unsigned Size = R.getMaxVecRegSize(); Size >= R.getMinVecRegSize();
4367          Size /= 2) {
4368       if (vectorizeStoreChain(Operands, R, Size)) {
4369         // Mark the vectorized stores so that we don't vectorize them again.
4370         VectorizedStores.insert(Operands.begin(), Operands.end());
4371         Changed = true;
4372         break;
4373       }
4374     }
4375   }
4376 
4377   return Changed;
4378 }
4379 
4380 void SLPVectorizerPass::collectSeedInstructions(BasicBlock *BB) {
4381   // Initialize the collections. We will make a single pass over the block.
4382   Stores.clear();
4383   GEPs.clear();
4384 
4385   // Visit the store and getelementptr instructions in BB and organize them in
4386   // Stores and GEPs according to the underlying objects of their pointer
4387   // operands.
4388   for (Instruction &I : *BB) {
4389     // Ignore store instructions that are volatile or have a pointer operand
4390     // that doesn't point to a scalar type.
4391     if (auto *SI = dyn_cast<StoreInst>(&I)) {
4392       if (!SI->isSimple())
4393         continue;
4394       if (!isValidElementType(SI->getValueOperand()->getType()))
4395         continue;
4396       Stores[GetUnderlyingObject(SI->getPointerOperand(), *DL)].push_back(SI);
4397     }
4398 
4399     // Ignore getelementptr instructions that have more than one index, a
4400     // constant index, or a pointer operand that doesn't point to a scalar
4401     // type.
4402     else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
4403       auto Idx = GEP->idx_begin()->get();
4404       if (GEP->getNumIndices() > 1 || isa<Constant>(Idx))
4405         continue;
4406       if (!isValidElementType(Idx->getType()))
4407         continue;
4408       if (GEP->getType()->isVectorTy())
4409         continue;
4410       GEPs[GetUnderlyingObject(GEP->getPointerOperand(), *DL)].push_back(GEP);
4411     }
4412   }
4413 }
4414 
4415 bool SLPVectorizerPass::tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) {
4416   if (!A || !B)
4417     return false;
4418   Value *VL[] = { A, B };
4419   return tryToVectorizeList(VL, R, true);
4420 }
4421 
4422 bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
4423                                            bool AllowReorder) {
4424   if (VL.size() < 2)
4425     return false;
4426 
4427   DEBUG(dbgs() << "SLP: Trying to vectorize a list of length = " << VL.size()
4428                << ".\n");
4429 
4430   // Check that all of the parts are scalar instructions of the same type.
4431   Instruction *I0 = dyn_cast<Instruction>(VL[0]);
4432   if (!I0)
4433     return false;
4434 
4435   unsigned Opcode0 = I0->getOpcode();
4436 
4437   unsigned Sz = R.getVectorElementSize(I0);
4438   unsigned MinVF = std::max(2U, R.getMinVecRegSize() / Sz);
4439   unsigned MaxVF = std::max<unsigned>(PowerOf2Floor(VL.size()), MinVF);
4440   if (MaxVF < 2) {
4441      R.getORE()->emit([&]() {
4442          return OptimizationRemarkMissed(
4443                     SV_NAME, "SmallVF", I0)
4444                 << "Cannot SLP vectorize list: vectorization factor "
4445                 << "less than 2 is not supported";
4446      });
4447      return false;
4448   }
4449 
4450   for (Value *V : VL) {
4451     Type *Ty = V->getType();
4452     if (!isValidElementType(Ty)) {
4453       // NOTE: the following will give user internal llvm type name, which may not be useful
4454       R.getORE()->emit([&]() {
4455           std::string type_str;
4456           llvm::raw_string_ostream rso(type_str);
4457           Ty->print(rso);
4458           return OptimizationRemarkMissed(
4459                      SV_NAME, "UnsupportedType", I0)
4460                  << "Cannot SLP vectorize list: type "
4461                  << rso.str() + " is unsupported by vectorizer";
4462       });
4463       return false;
4464     }
4465     Instruction *Inst = dyn_cast<Instruction>(V);
4466 
4467     if (!Inst)
4468         return false;
4469     if (Inst->getOpcode() != Opcode0) {
4470       R.getORE()->emit([&]() {
4471           return OptimizationRemarkMissed(
4472                      SV_NAME, "InequableTypes", I0)
4473                  << "Cannot SLP vectorize list: not all of the "
4474                  << "parts of scalar instructions are of the same type: "
4475                  << ore::NV("Instruction1Opcode", I0) << " and "
4476                  << ore::NV("Instruction2Opcode", Inst);
4477       });
4478       return false;
4479     }
4480   }
4481 
4482   bool Changed = false;
4483   bool CandidateFound = false;
4484   int MinCost = SLPCostThreshold;
4485 
4486   // Keep track of values that were deleted by vectorizing in the loop below.
4487   SmallVector<WeakTrackingVH, 8> TrackValues(VL.begin(), VL.end());
4488 
4489   unsigned NextInst = 0, MaxInst = VL.size();
4490   for (unsigned VF = MaxVF; NextInst + 1 < MaxInst && VF >= MinVF;
4491        VF /= 2) {
4492     // No actual vectorization should happen, if number of parts is the same as
4493     // provided vectorization factor (i.e. the scalar type is used for vector
4494     // code during codegen).
4495     auto *VecTy = VectorType::get(VL[0]->getType(), VF);
4496     if (TTI->getNumberOfParts(VecTy) == VF)
4497       continue;
4498     for (unsigned I = NextInst; I < MaxInst; ++I) {
4499       unsigned OpsWidth = 0;
4500 
4501       if (I + VF > MaxInst)
4502         OpsWidth = MaxInst - I;
4503       else
4504         OpsWidth = VF;
4505 
4506       if (!isPowerOf2_32(OpsWidth) || OpsWidth < 2)
4507         break;
4508 
4509       // Check that a previous iteration of this loop did not delete the Value.
4510       if (hasValueBeenRAUWed(VL, TrackValues, I, OpsWidth))
4511         continue;
4512 
4513       DEBUG(dbgs() << "SLP: Analyzing " << OpsWidth << " operations "
4514                    << "\n");
4515       ArrayRef<Value *> Ops = VL.slice(I, OpsWidth);
4516 
4517       R.buildTree(Ops);
4518       // TODO: check if we can allow reordering for more cases.
4519       if (AllowReorder && R.shouldReorder()) {
4520         // Conceptually, there is nothing actually preventing us from trying to
4521         // reorder a larger list. In fact, we do exactly this when vectorizing
4522         // reductions. However, at this point, we only expect to get here when
4523         // there are exactly two operations.
4524         assert(Ops.size() == 2);
4525         Value *ReorderedOps[] = {Ops[1], Ops[0]};
4526         R.buildTree(ReorderedOps, None);
4527       }
4528       if (R.isTreeTinyAndNotFullyVectorizable())
4529         continue;
4530 
4531       R.computeMinimumValueSizes();
4532       int Cost = R.getTreeCost();
4533       CandidateFound = true;
4534       MinCost = std::min(MinCost, Cost);
4535 
4536       if (Cost < -SLPCostThreshold) {
4537         DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
4538         R.getORE()->emit(OptimizationRemark(SV_NAME, "VectorizedList",
4539                                                     cast<Instruction>(Ops[0]))
4540                                  << "SLP vectorized with cost " << ore::NV("Cost", Cost)
4541                                  << " and with tree size "
4542                                  << ore::NV("TreeSize", R.getTreeSize()));
4543 
4544         R.vectorizeTree();
4545         // Move to the next bundle.
4546         I += VF - 1;
4547         NextInst = I + 1;
4548         Changed = true;
4549       }
4550     }
4551   }
4552 
4553   if (!Changed && CandidateFound) {
4554     R.getORE()->emit([&]() {
4555         return OptimizationRemarkMissed(
4556                    SV_NAME, "NotBeneficial",  I0)
4557                << "List vectorization was possible but not beneficial with cost "
4558                << ore::NV("Cost", MinCost) << " >= "
4559                << ore::NV("Treshold", -SLPCostThreshold);
4560     });
4561   } else if (!Changed) {
4562     R.getORE()->emit([&]() {
4563         return OptimizationRemarkMissed(
4564                    SV_NAME, "NotPossible", I0)
4565                << "Cannot SLP vectorize list: vectorization was impossible"
4566                << " with available vectorization factors";
4567     });
4568   }
4569   return Changed;
4570 }
4571 
4572 bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) {
4573   if (!I)
4574     return false;
4575 
4576   if (!isa<BinaryOperator>(I) && !isa<CmpInst>(I))
4577     return false;
4578 
4579   Value *P = I->getParent();
4580 
4581   // Vectorize in current basic block only.
4582   auto *Op0 = dyn_cast<Instruction>(I->getOperand(0));
4583   auto *Op1 = dyn_cast<Instruction>(I->getOperand(1));
4584   if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P)
4585     return false;
4586 
4587   // Try to vectorize V.
4588   if (tryToVectorizePair(Op0, Op1, R))
4589     return true;
4590 
4591   auto *A = dyn_cast<BinaryOperator>(Op0);
4592   auto *B = dyn_cast<BinaryOperator>(Op1);
4593   // Try to skip B.
4594   if (B && B->hasOneUse()) {
4595     auto *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
4596     auto *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
4597     if (B0 && B0->getParent() == P && tryToVectorizePair(A, B0, R))
4598       return true;
4599     if (B1 && B1->getParent() == P && tryToVectorizePair(A, B1, R))
4600       return true;
4601   }
4602 
4603   // Try to skip A.
4604   if (A && A->hasOneUse()) {
4605     auto *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
4606     auto *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
4607     if (A0 && A0->getParent() == P && tryToVectorizePair(A0, B, R))
4608       return true;
4609     if (A1 && A1->getParent() == P && tryToVectorizePair(A1, B, R))
4610       return true;
4611   }
4612   return false;
4613 }
4614 
4615 /// \brief Generate a shuffle mask to be used in a reduction tree.
4616 ///
4617 /// \param VecLen The length of the vector to be reduced.
4618 /// \param NumEltsToRdx The number of elements that should be reduced in the
4619 ///        vector.
4620 /// \param IsPairwise Whether the reduction is a pairwise or splitting
4621 ///        reduction. A pairwise reduction will generate a mask of
4622 ///        <0,2,...> or <1,3,..> while a splitting reduction will generate
4623 ///        <2,3, undef,undef> for a vector of 4 and NumElts = 2.
4624 /// \param IsLeft True will generate a mask of even elements, odd otherwise.
4625 static Value *createRdxShuffleMask(unsigned VecLen, unsigned NumEltsToRdx,
4626                                    bool IsPairwise, bool IsLeft,
4627                                    IRBuilder<> &Builder) {
4628   assert((IsPairwise || !IsLeft) && "Don't support a <0,1,undef,...> mask");
4629 
4630   SmallVector<Constant *, 32> ShuffleMask(
4631       VecLen, UndefValue::get(Builder.getInt32Ty()));
4632 
4633   if (IsPairwise)
4634     // Build a mask of 0, 2, ... (left) or 1, 3, ... (right).
4635     for (unsigned i = 0; i != NumEltsToRdx; ++i)
4636       ShuffleMask[i] = Builder.getInt32(2 * i + !IsLeft);
4637   else
4638     // Move the upper half of the vector to the lower half.
4639     for (unsigned i = 0; i != NumEltsToRdx; ++i)
4640       ShuffleMask[i] = Builder.getInt32(NumEltsToRdx + i);
4641 
4642   return ConstantVector::get(ShuffleMask);
4643 }
4644 
4645 namespace {
4646 
4647 /// Model horizontal reductions.
4648 ///
4649 /// A horizontal reduction is a tree of reduction operations (currently add and
4650 /// fadd) that has operations that can be put into a vector as its leaf.
4651 /// For example, this tree:
4652 ///
4653 /// mul mul mul mul
4654 ///  \  /    \  /
4655 ///   +       +
4656 ///    \     /
4657 ///       +
4658 /// This tree has "mul" as its reduced values and "+" as its reduction
4659 /// operations. A reduction might be feeding into a store or a binary operation
4660 /// feeding a phi.
4661 ///    ...
4662 ///    \  /
4663 ///     +
4664 ///     |
4665 ///  phi +=
4666 ///
4667 ///  Or:
4668 ///    ...
4669 ///    \  /
4670 ///     +
4671 ///     |
4672 ///   *p =
4673 ///
4674 class HorizontalReduction {
4675   using ReductionOpsType = SmallVector<Value *, 16>;
4676   using ReductionOpsListType = SmallVector<ReductionOpsType, 2>;
4677   ReductionOpsListType  ReductionOps;
4678   SmallVector<Value *, 32> ReducedVals;
4679   // Use map vector to make stable output.
4680   MapVector<Instruction *, Value *> ExtraArgs;
4681 
4682   /// Kind of the reduction data.
4683   enum ReductionKind {
4684     RK_None,       /// Not a reduction.
4685     RK_Arithmetic, /// Binary reduction data.
4686     RK_Min,        /// Minimum reduction data.
4687     RK_UMin,       /// Unsigned minimum reduction data.
4688     RK_Max,        /// Maximum reduction data.
4689     RK_UMax,       /// Unsigned maximum reduction data.
4690   };
4691 
4692   /// Contains info about operation, like its opcode, left and right operands.
4693   class OperationData {
4694     /// Opcode of the instruction.
4695     unsigned Opcode = 0;
4696 
4697     /// Left operand of the reduction operation.
4698     Value *LHS = nullptr;
4699 
4700     /// Right operand of the reduction operation.
4701     Value *RHS = nullptr;
4702 
4703     /// Kind of the reduction operation.
4704     ReductionKind Kind = RK_None;
4705 
4706     /// True if float point min/max reduction has no NaNs.
4707     bool NoNaN = false;
4708 
4709     /// Checks if the reduction operation can be vectorized.
4710     bool isVectorizable() const {
4711       return LHS && RHS &&
4712              // We currently only support adds && min/max reductions.
4713              ((Kind == RK_Arithmetic &&
4714                (Opcode == Instruction::Add || Opcode == Instruction::FAdd)) ||
4715               ((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
4716                (Kind == RK_Min || Kind == RK_Max)) ||
4717               (Opcode == Instruction::ICmp &&
4718                (Kind == RK_UMin || Kind == RK_UMax)));
4719     }
4720 
4721     /// Creates reduction operation with the current opcode.
4722     Value *createOp(IRBuilder<> &Builder, const Twine &Name) const {
4723       assert(isVectorizable() &&
4724              "Expected add|fadd or min/max reduction operation.");
4725       Value *Cmp;
4726       switch (Kind) {
4727       case RK_Arithmetic:
4728         return Builder.CreateBinOp((Instruction::BinaryOps)Opcode, LHS, RHS,
4729                                    Name);
4730       case RK_Min:
4731         Cmp = Opcode == Instruction::ICmp ? Builder.CreateICmpSLT(LHS, RHS)
4732                                           : Builder.CreateFCmpOLT(LHS, RHS);
4733         break;
4734       case RK_Max:
4735         Cmp = Opcode == Instruction::ICmp ? Builder.CreateICmpSGT(LHS, RHS)
4736                                           : Builder.CreateFCmpOGT(LHS, RHS);
4737         break;
4738       case RK_UMin:
4739         assert(Opcode == Instruction::ICmp && "Expected integer types.");
4740         Cmp = Builder.CreateICmpULT(LHS, RHS);
4741         break;
4742       case RK_UMax:
4743         assert(Opcode == Instruction::ICmp && "Expected integer types.");
4744         Cmp = Builder.CreateICmpUGT(LHS, RHS);
4745         break;
4746       case RK_None:
4747         llvm_unreachable("Unknown reduction operation.");
4748       }
4749       return Builder.CreateSelect(Cmp, LHS, RHS, Name);
4750     }
4751 
4752   public:
4753     explicit OperationData() = default;
4754 
4755     /// Construction for reduced values. They are identified by opcode only and
4756     /// don't have associated LHS/RHS values.
4757     explicit OperationData(Value *V) {
4758       if (auto *I = dyn_cast<Instruction>(V))
4759         Opcode = I->getOpcode();
4760     }
4761 
4762     /// Constructor for reduction operations with opcode and its left and
4763     /// right operands.
4764     OperationData(unsigned Opcode, Value *LHS, Value *RHS, ReductionKind Kind,
4765                   bool NoNaN = false)
4766         : Opcode(Opcode), LHS(LHS), RHS(RHS), Kind(Kind), NoNaN(NoNaN) {
4767       assert(Kind != RK_None && "One of the reduction operations is expected.");
4768     }
4769 
4770     explicit operator bool() const { return Opcode; }
4771 
4772     /// Get the index of the first operand.
4773     unsigned getFirstOperandIndex() const {
4774       assert(!!*this && "The opcode is not set.");
4775       switch (Kind) {
4776       case RK_Min:
4777       case RK_UMin:
4778       case RK_Max:
4779       case RK_UMax:
4780         return 1;
4781       case RK_Arithmetic:
4782       case RK_None:
4783         break;
4784       }
4785       return 0;
4786     }
4787 
4788     /// Total number of operands in the reduction operation.
4789     unsigned getNumberOfOperands() const {
4790       assert(Kind != RK_None && !!*this && LHS && RHS &&
4791              "Expected reduction operation.");
4792       switch (Kind) {
4793       case RK_Arithmetic:
4794         return 2;
4795       case RK_Min:
4796       case RK_UMin:
4797       case RK_Max:
4798       case RK_UMax:
4799         return 3;
4800       case RK_None:
4801         break;
4802       }
4803       llvm_unreachable("Reduction kind is not set");
4804     }
4805 
4806     /// Checks if the operation has the same parent as \p P.
4807     bool hasSameParent(Instruction *I, Value *P, bool IsRedOp) const {
4808       assert(Kind != RK_None && !!*this && LHS && RHS &&
4809              "Expected reduction operation.");
4810       if (!IsRedOp)
4811         return I->getParent() == P;
4812       switch (Kind) {
4813       case RK_Arithmetic:
4814         // Arithmetic reduction operation must be used once only.
4815         return I->getParent() == P;
4816       case RK_Min:
4817       case RK_UMin:
4818       case RK_Max:
4819       case RK_UMax: {
4820         // SelectInst must be used twice while the condition op must have single
4821         // use only.
4822         auto *Cmp = cast<Instruction>(cast<SelectInst>(I)->getCondition());
4823         return I->getParent() == P && Cmp && Cmp->getParent() == P;
4824       }
4825       case RK_None:
4826         break;
4827       }
4828       llvm_unreachable("Reduction kind is not set");
4829     }
4830     /// Expected number of uses for reduction operations/reduced values.
4831     bool hasRequiredNumberOfUses(Instruction *I, bool IsReductionOp) const {
4832       assert(Kind != RK_None && !!*this && LHS && RHS &&
4833              "Expected reduction operation.");
4834       switch (Kind) {
4835       case RK_Arithmetic:
4836         return I->hasOneUse();
4837       case RK_Min:
4838       case RK_UMin:
4839       case RK_Max:
4840       case RK_UMax:
4841         return I->hasNUses(2) &&
4842                (!IsReductionOp ||
4843                 cast<SelectInst>(I)->getCondition()->hasOneUse());
4844       case RK_None:
4845         break;
4846       }
4847       llvm_unreachable("Reduction kind is not set");
4848     }
4849 
4850     /// Initializes the list of reduction operations.
4851     void initReductionOps(ReductionOpsListType &ReductionOps) {
4852       assert(Kind != RK_None && !!*this && LHS && RHS &&
4853              "Expected reduction operation.");
4854       switch (Kind) {
4855       case RK_Arithmetic:
4856         ReductionOps.assign(1, ReductionOpsType());
4857         break;
4858       case RK_Min:
4859       case RK_UMin:
4860       case RK_Max:
4861       case RK_UMax:
4862         ReductionOps.assign(2, ReductionOpsType());
4863         break;
4864       case RK_None:
4865         llvm_unreachable("Reduction kind is not set");
4866       }
4867     }
4868     /// Add all reduction operations for the reduction instruction \p I.
4869     void addReductionOps(Instruction *I, ReductionOpsListType &ReductionOps) {
4870       assert(Kind != RK_None && !!*this && LHS && RHS &&
4871              "Expected reduction operation.");
4872       switch (Kind) {
4873       case RK_Arithmetic:
4874         ReductionOps[0].emplace_back(I);
4875         break;
4876       case RK_Min:
4877       case RK_UMin:
4878       case RK_Max:
4879       case RK_UMax:
4880         ReductionOps[0].emplace_back(cast<SelectInst>(I)->getCondition());
4881         ReductionOps[1].emplace_back(I);
4882         break;
4883       case RK_None:
4884         llvm_unreachable("Reduction kind is not set");
4885       }
4886     }
4887 
4888     /// Checks if instruction is associative and can be vectorized.
4889     bool isAssociative(Instruction *I) const {
4890       assert(Kind != RK_None && *this && LHS && RHS &&
4891              "Expected reduction operation.");
4892       switch (Kind) {
4893       case RK_Arithmetic:
4894         return I->isAssociative();
4895       case RK_Min:
4896       case RK_Max:
4897         return Opcode == Instruction::ICmp ||
4898                cast<Instruction>(I->getOperand(0))->isFast();
4899       case RK_UMin:
4900       case RK_UMax:
4901         assert(Opcode == Instruction::ICmp &&
4902                "Only integer compare operation is expected.");
4903         return true;
4904       case RK_None:
4905         break;
4906       }
4907       llvm_unreachable("Reduction kind is not set");
4908     }
4909 
4910     /// Checks if the reduction operation can be vectorized.
4911     bool isVectorizable(Instruction *I) const {
4912       return isVectorizable() && isAssociative(I);
4913     }
4914 
4915     /// Checks if two operation data are both a reduction op or both a reduced
4916     /// value.
4917     bool operator==(const OperationData &OD) {
4918       assert(((Kind != OD.Kind) || ((!LHS == !OD.LHS) && (!RHS == !OD.RHS))) &&
4919              "One of the comparing operations is incorrect.");
4920       return this == &OD || (Kind == OD.Kind && Opcode == OD.Opcode);
4921     }
4922     bool operator!=(const OperationData &OD) { return !(*this == OD); }
4923     void clear() {
4924       Opcode = 0;
4925       LHS = nullptr;
4926       RHS = nullptr;
4927       Kind = RK_None;
4928       NoNaN = false;
4929     }
4930 
4931     /// Get the opcode of the reduction operation.
4932     unsigned getOpcode() const {
4933       assert(isVectorizable() && "Expected vectorizable operation.");
4934       return Opcode;
4935     }
4936 
4937     /// Get kind of reduction data.
4938     ReductionKind getKind() const { return Kind; }
4939     Value *getLHS() const { return LHS; }
4940     Value *getRHS() const { return RHS; }
4941     Type *getConditionType() const {
4942       switch (Kind) {
4943       case RK_Arithmetic:
4944         return nullptr;
4945       case RK_Min:
4946       case RK_Max:
4947       case RK_UMin:
4948       case RK_UMax:
4949         return CmpInst::makeCmpResultType(LHS->getType());
4950       case RK_None:
4951         break;
4952       }
4953       llvm_unreachable("Reduction kind is not set");
4954     }
4955 
4956     /// Creates reduction operation with the current opcode with the IR flags
4957     /// from \p ReductionOps.
4958     Value *createOp(IRBuilder<> &Builder, const Twine &Name,
4959                     const ReductionOpsListType &ReductionOps) const {
4960       assert(isVectorizable() &&
4961              "Expected add|fadd or min/max reduction operation.");
4962       auto *Op = createOp(Builder, Name);
4963       switch (Kind) {
4964       case RK_Arithmetic:
4965         propagateIRFlags(Op, ReductionOps[0]);
4966         return Op;
4967       case RK_Min:
4968       case RK_Max:
4969       case RK_UMin:
4970       case RK_UMax:
4971         if (auto *SI = dyn_cast<SelectInst>(Op))
4972           propagateIRFlags(SI->getCondition(), ReductionOps[0]);
4973         propagateIRFlags(Op, ReductionOps[1]);
4974         return Op;
4975       case RK_None:
4976         break;
4977       }
4978       llvm_unreachable("Unknown reduction operation.");
4979     }
4980     /// Creates reduction operation with the current opcode with the IR flags
4981     /// from \p I.
4982     Value *createOp(IRBuilder<> &Builder, const Twine &Name,
4983                     Instruction *I) const {
4984       assert(isVectorizable() &&
4985              "Expected add|fadd or min/max reduction operation.");
4986       auto *Op = createOp(Builder, Name);
4987       switch (Kind) {
4988       case RK_Arithmetic:
4989         propagateIRFlags(Op, I);
4990         return Op;
4991       case RK_Min:
4992       case RK_Max:
4993       case RK_UMin:
4994       case RK_UMax:
4995         if (auto *SI = dyn_cast<SelectInst>(Op)) {
4996           propagateIRFlags(SI->getCondition(),
4997                            cast<SelectInst>(I)->getCondition());
4998         }
4999         propagateIRFlags(Op, I);
5000         return Op;
5001       case RK_None:
5002         break;
5003       }
5004       llvm_unreachable("Unknown reduction operation.");
5005     }
5006 
5007     TargetTransformInfo::ReductionFlags getFlags() const {
5008       TargetTransformInfo::ReductionFlags Flags;
5009       Flags.NoNaN = NoNaN;
5010       switch (Kind) {
5011       case RK_Arithmetic:
5012         break;
5013       case RK_Min:
5014         Flags.IsSigned = Opcode == Instruction::ICmp;
5015         Flags.IsMaxOp = false;
5016         break;
5017       case RK_Max:
5018         Flags.IsSigned = Opcode == Instruction::ICmp;
5019         Flags.IsMaxOp = true;
5020         break;
5021       case RK_UMin:
5022         Flags.IsSigned = false;
5023         Flags.IsMaxOp = false;
5024         break;
5025       case RK_UMax:
5026         Flags.IsSigned = false;
5027         Flags.IsMaxOp = true;
5028         break;
5029       case RK_None:
5030         llvm_unreachable("Reduction kind is not set");
5031       }
5032       return Flags;
5033     }
5034   };
5035 
5036   Instruction *ReductionRoot = nullptr;
5037 
5038   /// The operation data of the reduction operation.
5039   OperationData ReductionData;
5040 
5041   /// The operation data of the values we perform a reduction on.
5042   OperationData ReducedValueData;
5043 
5044   /// Should we model this reduction as a pairwise reduction tree or a tree that
5045   /// splits the vector in halves and adds those halves.
5046   bool IsPairwiseReduction = false;
5047 
5048   /// Checks if the ParentStackElem.first should be marked as a reduction
5049   /// operation with an extra argument or as extra argument itself.
5050   void markExtraArg(std::pair<Instruction *, unsigned> &ParentStackElem,
5051                     Value *ExtraArg) {
5052     if (ExtraArgs.count(ParentStackElem.first)) {
5053       ExtraArgs[ParentStackElem.first] = nullptr;
5054       // We ran into something like:
5055       // ParentStackElem.first = ExtraArgs[ParentStackElem.first] + ExtraArg.
5056       // The whole ParentStackElem.first should be considered as an extra value
5057       // in this case.
5058       // Do not perform analysis of remaining operands of ParentStackElem.first
5059       // instruction, this whole instruction is an extra argument.
5060       ParentStackElem.second = ParentStackElem.first->getNumOperands();
5061     } else {
5062       // We ran into something like:
5063       // ParentStackElem.first += ... + ExtraArg + ...
5064       ExtraArgs[ParentStackElem.first] = ExtraArg;
5065     }
5066   }
5067 
5068   static OperationData getOperationData(Value *V) {
5069     if (!V)
5070       return OperationData();
5071 
5072     Value *LHS;
5073     Value *RHS;
5074     if (m_BinOp(m_Value(LHS), m_Value(RHS)).match(V)) {
5075       return OperationData(cast<BinaryOperator>(V)->getOpcode(), LHS, RHS,
5076                            RK_Arithmetic);
5077     }
5078     if (auto *Select = dyn_cast<SelectInst>(V)) {
5079       // Look for a min/max pattern.
5080       if (m_UMin(m_Value(LHS), m_Value(RHS)).match(Select)) {
5081         return OperationData(Instruction::ICmp, LHS, RHS, RK_UMin);
5082       } else if (m_SMin(m_Value(LHS), m_Value(RHS)).match(Select)) {
5083         return OperationData(Instruction::ICmp, LHS, RHS, RK_Min);
5084       } else if (m_OrdFMin(m_Value(LHS), m_Value(RHS)).match(Select) ||
5085                  m_UnordFMin(m_Value(LHS), m_Value(RHS)).match(Select)) {
5086         return OperationData(
5087             Instruction::FCmp, LHS, RHS, RK_Min,
5088             cast<Instruction>(Select->getCondition())->hasNoNaNs());
5089       } else if (m_UMax(m_Value(LHS), m_Value(RHS)).match(Select)) {
5090         return OperationData(Instruction::ICmp, LHS, RHS, RK_UMax);
5091       } else if (m_SMax(m_Value(LHS), m_Value(RHS)).match(Select)) {
5092         return OperationData(Instruction::ICmp, LHS, RHS, RK_Max);
5093       } else if (m_OrdFMax(m_Value(LHS), m_Value(RHS)).match(Select) ||
5094                  m_UnordFMax(m_Value(LHS), m_Value(RHS)).match(Select)) {
5095         return OperationData(
5096             Instruction::FCmp, LHS, RHS, RK_Max,
5097             cast<Instruction>(Select->getCondition())->hasNoNaNs());
5098       }
5099     }
5100     return OperationData(V);
5101   }
5102 
5103 public:
5104   HorizontalReduction() = default;
5105 
5106   /// \brief Try to find a reduction tree.
5107   bool matchAssociativeReduction(PHINode *Phi, Instruction *B) {
5108     assert((!Phi || is_contained(Phi->operands(), B)) &&
5109            "Thi phi needs to use the binary operator");
5110 
5111     ReductionData = getOperationData(B);
5112 
5113     // We could have a initial reductions that is not an add.
5114     //  r *= v1 + v2 + v3 + v4
5115     // In such a case start looking for a tree rooted in the first '+'.
5116     if (Phi) {
5117       if (ReductionData.getLHS() == Phi) {
5118         Phi = nullptr;
5119         B = dyn_cast<Instruction>(ReductionData.getRHS());
5120         ReductionData = getOperationData(B);
5121       } else if (ReductionData.getRHS() == Phi) {
5122         Phi = nullptr;
5123         B = dyn_cast<Instruction>(ReductionData.getLHS());
5124         ReductionData = getOperationData(B);
5125       }
5126     }
5127 
5128     if (!ReductionData.isVectorizable(B))
5129       return false;
5130 
5131     Type *Ty = B->getType();
5132     if (!isValidElementType(Ty))
5133       return false;
5134 
5135     ReducedValueData.clear();
5136     ReductionRoot = B;
5137 
5138     // Post order traverse the reduction tree starting at B. We only handle true
5139     // trees containing only binary operators.
5140     SmallVector<std::pair<Instruction *, unsigned>, 32> Stack;
5141     Stack.push_back(std::make_pair(B, ReductionData.getFirstOperandIndex()));
5142     ReductionData.initReductionOps(ReductionOps);
5143     while (!Stack.empty()) {
5144       Instruction *TreeN = Stack.back().first;
5145       unsigned EdgeToVist = Stack.back().second++;
5146       OperationData OpData = getOperationData(TreeN);
5147       bool IsReducedValue = OpData != ReductionData;
5148 
5149       // Postorder vist.
5150       if (IsReducedValue || EdgeToVist == OpData.getNumberOfOperands()) {
5151         if (IsReducedValue)
5152           ReducedVals.push_back(TreeN);
5153         else {
5154           auto I = ExtraArgs.find(TreeN);
5155           if (I != ExtraArgs.end() && !I->second) {
5156             // Check if TreeN is an extra argument of its parent operation.
5157             if (Stack.size() <= 1) {
5158               // TreeN can't be an extra argument as it is a root reduction
5159               // operation.
5160               return false;
5161             }
5162             // Yes, TreeN is an extra argument, do not add it to a list of
5163             // reduction operations.
5164             // Stack[Stack.size() - 2] always points to the parent operation.
5165             markExtraArg(Stack[Stack.size() - 2], TreeN);
5166             ExtraArgs.erase(TreeN);
5167           } else
5168             ReductionData.addReductionOps(TreeN, ReductionOps);
5169         }
5170         // Retract.
5171         Stack.pop_back();
5172         continue;
5173       }
5174 
5175       // Visit left or right.
5176       Value *NextV = TreeN->getOperand(EdgeToVist);
5177       if (NextV != Phi) {
5178         auto *I = dyn_cast<Instruction>(NextV);
5179         OpData = getOperationData(I);
5180         // Continue analysis if the next operand is a reduction operation or
5181         // (possibly) a reduced value. If the reduced value opcode is not set,
5182         // the first met operation != reduction operation is considered as the
5183         // reduced value class.
5184         if (I && (!ReducedValueData || OpData == ReducedValueData ||
5185                   OpData == ReductionData)) {
5186           const bool IsReductionOperation = OpData == ReductionData;
5187           // Only handle trees in the current basic block.
5188           if (!ReductionData.hasSameParent(I, B->getParent(),
5189                                            IsReductionOperation)) {
5190             // I is an extra argument for TreeN (its parent operation).
5191             markExtraArg(Stack.back(), I);
5192             continue;
5193           }
5194 
5195           // Each tree node needs to have minimal number of users except for the
5196           // ultimate reduction.
5197           if (!ReductionData.hasRequiredNumberOfUses(I,
5198                                                      OpData == ReductionData) &&
5199               I != B) {
5200             // I is an extra argument for TreeN (its parent operation).
5201             markExtraArg(Stack.back(), I);
5202             continue;
5203           }
5204 
5205           if (IsReductionOperation) {
5206             // We need to be able to reassociate the reduction operations.
5207             if (!OpData.isAssociative(I)) {
5208               // I is an extra argument for TreeN (its parent operation).
5209               markExtraArg(Stack.back(), I);
5210               continue;
5211             }
5212           } else if (ReducedValueData &&
5213                      ReducedValueData != OpData) {
5214             // Make sure that the opcodes of the operations that we are going to
5215             // reduce match.
5216             // I is an extra argument for TreeN (its parent operation).
5217             markExtraArg(Stack.back(), I);
5218             continue;
5219           } else if (!ReducedValueData)
5220             ReducedValueData = OpData;
5221 
5222           Stack.push_back(std::make_pair(I, OpData.getFirstOperandIndex()));
5223           continue;
5224         }
5225       }
5226       // NextV is an extra argument for TreeN (its parent operation).
5227       markExtraArg(Stack.back(), NextV);
5228     }
5229     return true;
5230   }
5231 
5232   /// \brief Attempt to vectorize the tree found by
5233   /// matchAssociativeReduction.
5234   bool tryToReduce(BoUpSLP &V, TargetTransformInfo *TTI) {
5235     if (ReducedVals.empty())
5236       return false;
5237 
5238     // If there is a sufficient number of reduction values, reduce
5239     // to a nearby power-of-2. Can safely generate oversized
5240     // vectors and rely on the backend to split them to legal sizes.
5241     unsigned NumReducedVals = ReducedVals.size();
5242     if (NumReducedVals < 4)
5243       return false;
5244 
5245     unsigned ReduxWidth = PowerOf2Floor(NumReducedVals);
5246 
5247     Value *VectorizedTree = nullptr;
5248     IRBuilder<> Builder(ReductionRoot);
5249     FastMathFlags Unsafe;
5250     Unsafe.setFast();
5251     Builder.setFastMathFlags(Unsafe);
5252     unsigned i = 0;
5253 
5254     BoUpSLP::ExtraValueToDebugLocsMap ExternallyUsedValues;
5255     // The same extra argument may be used several time, so log each attempt
5256     // to use it.
5257     for (auto &Pair : ExtraArgs)
5258       ExternallyUsedValues[Pair.second].push_back(Pair.first);
5259     SmallVector<Value *, 16> IgnoreList;
5260     for (auto &V : ReductionOps)
5261       IgnoreList.append(V.begin(), V.end());
5262     while (i < NumReducedVals - ReduxWidth + 1 && ReduxWidth > 2) {
5263       auto VL = makeArrayRef(&ReducedVals[i], ReduxWidth);
5264       V.buildTree(VL, ExternallyUsedValues, IgnoreList);
5265       if (V.shouldReorder()) {
5266         SmallVector<Value *, 8> Reversed(VL.rbegin(), VL.rend());
5267         V.buildTree(Reversed, ExternallyUsedValues, IgnoreList);
5268       }
5269       if (V.isTreeTinyAndNotFullyVectorizable())
5270         break;
5271 
5272       V.computeMinimumValueSizes();
5273 
5274       // Estimate cost.
5275       int Cost =
5276           V.getTreeCost() + getReductionCost(TTI, ReducedVals[i], ReduxWidth);
5277       if (Cost >= -SLPCostThreshold) {
5278           V.getORE()->emit([&]() {
5279               return OptimizationRemarkMissed(
5280                          SV_NAME, "HorSLPNotBeneficial", cast<Instruction>(VL[0]))
5281                      << "Vectorizing horizontal reduction is possible"
5282                      << "but not beneficial with cost "
5283                      << ore::NV("Cost", Cost) << " and threshold "
5284                      << ore::NV("Threshold", -SLPCostThreshold);
5285           });
5286           break;
5287       }
5288 
5289       DEBUG(dbgs() << "SLP: Vectorizing horizontal reduction at cost:" << Cost
5290                    << ". (HorRdx)\n");
5291       V.getORE()->emit([&]() {
5292           return OptimizationRemark(
5293                      SV_NAME, "VectorizedHorizontalReduction", cast<Instruction>(VL[0]))
5294           << "Vectorized horizontal reduction with cost "
5295           << ore::NV("Cost", Cost) << " and with tree size "
5296           << ore::NV("TreeSize", V.getTreeSize());
5297       });
5298 
5299       // Vectorize a tree.
5300       DebugLoc Loc = cast<Instruction>(ReducedVals[i])->getDebugLoc();
5301       Value *VectorizedRoot = V.vectorizeTree(ExternallyUsedValues);
5302 
5303       // Emit a reduction.
5304       Value *ReducedSubTree =
5305           emitReduction(VectorizedRoot, Builder, ReduxWidth, TTI);
5306       if (VectorizedTree) {
5307         Builder.SetCurrentDebugLocation(Loc);
5308         OperationData VectReductionData(ReductionData.getOpcode(),
5309                                         VectorizedTree, ReducedSubTree,
5310                                         ReductionData.getKind());
5311         VectorizedTree =
5312             VectReductionData.createOp(Builder, "op.rdx", ReductionOps);
5313       } else
5314         VectorizedTree = ReducedSubTree;
5315       i += ReduxWidth;
5316       ReduxWidth = PowerOf2Floor(NumReducedVals - i);
5317     }
5318 
5319     if (VectorizedTree) {
5320       // Finish the reduction.
5321       for (; i < NumReducedVals; ++i) {
5322         auto *I = cast<Instruction>(ReducedVals[i]);
5323         Builder.SetCurrentDebugLocation(I->getDebugLoc());
5324         OperationData VectReductionData(ReductionData.getOpcode(),
5325                                         VectorizedTree, I,
5326                                         ReductionData.getKind());
5327         VectorizedTree = VectReductionData.createOp(Builder, "", ReductionOps);
5328       }
5329       for (auto &Pair : ExternallyUsedValues) {
5330         assert(!Pair.second.empty() &&
5331                "At least one DebugLoc must be inserted");
5332         // Add each externally used value to the final reduction.
5333         for (auto *I : Pair.second) {
5334           Builder.SetCurrentDebugLocation(I->getDebugLoc());
5335           OperationData VectReductionData(ReductionData.getOpcode(),
5336                                           VectorizedTree, Pair.first,
5337                                           ReductionData.getKind());
5338           VectorizedTree = VectReductionData.createOp(Builder, "op.extra", I);
5339         }
5340       }
5341       // Update users.
5342       ReductionRoot->replaceAllUsesWith(VectorizedTree);
5343     }
5344     return VectorizedTree != nullptr;
5345   }
5346 
5347   unsigned numReductionValues() const {
5348     return ReducedVals.size();
5349   }
5350 
5351 private:
5352   /// \brief Calculate the cost of a reduction.
5353   int getReductionCost(TargetTransformInfo *TTI, Value *FirstReducedVal,
5354                        unsigned ReduxWidth) {
5355     Type *ScalarTy = FirstReducedVal->getType();
5356     Type *VecTy = VectorType::get(ScalarTy, ReduxWidth);
5357 
5358     int PairwiseRdxCost;
5359     int SplittingRdxCost;
5360     switch (ReductionData.getKind()) {
5361     case RK_Arithmetic:
5362       PairwiseRdxCost =
5363           TTI->getArithmeticReductionCost(ReductionData.getOpcode(), VecTy,
5364                                           /*IsPairwiseForm=*/true);
5365       SplittingRdxCost =
5366           TTI->getArithmeticReductionCost(ReductionData.getOpcode(), VecTy,
5367                                           /*IsPairwiseForm=*/false);
5368       break;
5369     case RK_Min:
5370     case RK_Max:
5371     case RK_UMin:
5372     case RK_UMax: {
5373       Type *VecCondTy = CmpInst::makeCmpResultType(VecTy);
5374       bool IsUnsigned = ReductionData.getKind() == RK_UMin ||
5375                         ReductionData.getKind() == RK_UMax;
5376       PairwiseRdxCost =
5377           TTI->getMinMaxReductionCost(VecTy, VecCondTy,
5378                                       /*IsPairwiseForm=*/true, IsUnsigned);
5379       SplittingRdxCost =
5380           TTI->getMinMaxReductionCost(VecTy, VecCondTy,
5381                                       /*IsPairwiseForm=*/false, IsUnsigned);
5382       break;
5383     }
5384     case RK_None:
5385       llvm_unreachable("Expected arithmetic or min/max reduction operation");
5386     }
5387 
5388     IsPairwiseReduction = PairwiseRdxCost < SplittingRdxCost;
5389     int VecReduxCost = IsPairwiseReduction ? PairwiseRdxCost : SplittingRdxCost;
5390 
5391     int ScalarReduxCost;
5392     switch (ReductionData.getKind()) {
5393     case RK_Arithmetic:
5394       ScalarReduxCost =
5395           TTI->getArithmeticInstrCost(ReductionData.getOpcode(), ScalarTy);
5396       break;
5397     case RK_Min:
5398     case RK_Max:
5399     case RK_UMin:
5400     case RK_UMax:
5401       ScalarReduxCost =
5402           TTI->getCmpSelInstrCost(ReductionData.getOpcode(), ScalarTy) +
5403           TTI->getCmpSelInstrCost(Instruction::Select, ScalarTy,
5404                                   CmpInst::makeCmpResultType(ScalarTy));
5405       break;
5406     case RK_None:
5407       llvm_unreachable("Expected arithmetic or min/max reduction operation");
5408     }
5409     ScalarReduxCost *= (ReduxWidth - 1);
5410 
5411     DEBUG(dbgs() << "SLP: Adding cost " << VecReduxCost - ScalarReduxCost
5412                  << " for reduction that starts with " << *FirstReducedVal
5413                  << " (It is a "
5414                  << (IsPairwiseReduction ? "pairwise" : "splitting")
5415                  << " reduction)\n");
5416 
5417     return VecReduxCost - ScalarReduxCost;
5418   }
5419 
5420   /// \brief Emit a horizontal reduction of the vectorized value.
5421   Value *emitReduction(Value *VectorizedValue, IRBuilder<> &Builder,
5422                        unsigned ReduxWidth, const TargetTransformInfo *TTI) {
5423     assert(VectorizedValue && "Need to have a vectorized tree node");
5424     assert(isPowerOf2_32(ReduxWidth) &&
5425            "We only handle power-of-two reductions for now");
5426 
5427     if (!IsPairwiseReduction)
5428       return createSimpleTargetReduction(
5429           Builder, TTI, ReductionData.getOpcode(), VectorizedValue,
5430           ReductionData.getFlags(), ReductionOps.back());
5431 
5432     Value *TmpVec = VectorizedValue;
5433     for (unsigned i = ReduxWidth / 2; i != 0; i >>= 1) {
5434       Value *LeftMask =
5435           createRdxShuffleMask(ReduxWidth, i, true, true, Builder);
5436       Value *RightMask =
5437           createRdxShuffleMask(ReduxWidth, i, true, false, Builder);
5438 
5439       Value *LeftShuf = Builder.CreateShuffleVector(
5440           TmpVec, UndefValue::get(TmpVec->getType()), LeftMask, "rdx.shuf.l");
5441       Value *RightShuf = Builder.CreateShuffleVector(
5442           TmpVec, UndefValue::get(TmpVec->getType()), (RightMask),
5443           "rdx.shuf.r");
5444       OperationData VectReductionData(ReductionData.getOpcode(), LeftShuf,
5445                                       RightShuf, ReductionData.getKind());
5446       TmpVec = VectReductionData.createOp(Builder, "op.rdx", ReductionOps);
5447     }
5448 
5449     // The result is in the first element of the vector.
5450     return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
5451   }
5452 };
5453 
5454 } // end anonymous namespace
5455 
5456 /// \brief Recognize construction of vectors like
5457 ///  %ra = insertelement <4 x float> undef, float %s0, i32 0
5458 ///  %rb = insertelement <4 x float> %ra, float %s1, i32 1
5459 ///  %rc = insertelement <4 x float> %rb, float %s2, i32 2
5460 ///  %rd = insertelement <4 x float> %rc, float %s3, i32 3
5461 ///  starting from the last insertelement instruction.
5462 ///
5463 /// Returns true if it matches
5464 static bool findBuildVector(InsertElementInst *LastInsertElem,
5465                             SmallVectorImpl<Value *> &BuildVectorOpds) {
5466   Value *V = nullptr;
5467   do {
5468     BuildVectorOpds.push_back(LastInsertElem->getOperand(1));
5469     V = LastInsertElem->getOperand(0);
5470     if (isa<UndefValue>(V))
5471       break;
5472     LastInsertElem = dyn_cast<InsertElementInst>(V);
5473     if (!LastInsertElem || !LastInsertElem->hasOneUse())
5474       return false;
5475   } while (true);
5476   std::reverse(BuildVectorOpds.begin(), BuildVectorOpds.end());
5477   return true;
5478 }
5479 
5480 /// \brief Like findBuildVector, but looks for construction of aggregate.
5481 ///
5482 /// \return true if it matches.
5483 static bool findBuildAggregate(InsertValueInst *IV,
5484                                SmallVectorImpl<Value *> &BuildVectorOpds) {
5485   Value *V;
5486   do {
5487     BuildVectorOpds.push_back(IV->getInsertedValueOperand());
5488     V = IV->getAggregateOperand();
5489     if (isa<UndefValue>(V))
5490       break;
5491     IV = dyn_cast<InsertValueInst>(V);
5492     if (!IV || !IV->hasOneUse())
5493       return false;
5494   } while (true);
5495   std::reverse(BuildVectorOpds.begin(), BuildVectorOpds.end());
5496   return true;
5497 }
5498 
5499 static bool PhiTypeSorterFunc(Value *V, Value *V2) {
5500   return V->getType() < V2->getType();
5501 }
5502 
5503 /// \brief Try and get a reduction value from a phi node.
5504 ///
5505 /// Given a phi node \p P in a block \p ParentBB, consider possible reductions
5506 /// if they come from either \p ParentBB or a containing loop latch.
5507 ///
5508 /// \returns A candidate reduction value if possible, or \code nullptr \endcode
5509 /// if not possible.
5510 static Value *getReductionValue(const DominatorTree *DT, PHINode *P,
5511                                 BasicBlock *ParentBB, LoopInfo *LI) {
5512   // There are situations where the reduction value is not dominated by the
5513   // reduction phi. Vectorizing such cases has been reported to cause
5514   // miscompiles. See PR25787.
5515   auto DominatedReduxValue = [&](Value *R) {
5516     return (
5517         dyn_cast<Instruction>(R) &&
5518         DT->dominates(P->getParent(), dyn_cast<Instruction>(R)->getParent()));
5519   };
5520 
5521   Value *Rdx = nullptr;
5522 
5523   // Return the incoming value if it comes from the same BB as the phi node.
5524   if (P->getIncomingBlock(0) == ParentBB) {
5525     Rdx = P->getIncomingValue(0);
5526   } else if (P->getIncomingBlock(1) == ParentBB) {
5527     Rdx = P->getIncomingValue(1);
5528   }
5529 
5530   if (Rdx && DominatedReduxValue(Rdx))
5531     return Rdx;
5532 
5533   // Otherwise, check whether we have a loop latch to look at.
5534   Loop *BBL = LI->getLoopFor(ParentBB);
5535   if (!BBL)
5536     return nullptr;
5537   BasicBlock *BBLatch = BBL->getLoopLatch();
5538   if (!BBLatch)
5539     return nullptr;
5540 
5541   // There is a loop latch, return the incoming value if it comes from
5542   // that. This reduction pattern occasionally turns up.
5543   if (P->getIncomingBlock(0) == BBLatch) {
5544     Rdx = P->getIncomingValue(0);
5545   } else if (P->getIncomingBlock(1) == BBLatch) {
5546     Rdx = P->getIncomingValue(1);
5547   }
5548 
5549   if (Rdx && DominatedReduxValue(Rdx))
5550     return Rdx;
5551 
5552   return nullptr;
5553 }
5554 
5555 /// Attempt to reduce a horizontal reduction.
5556 /// If it is legal to match a horizontal reduction feeding the phi node \a P
5557 /// with reduction operators \a Root (or one of its operands) in a basic block
5558 /// \a BB, then check if it can be done. If horizontal reduction is not found
5559 /// and root instruction is a binary operation, vectorization of the operands is
5560 /// attempted.
5561 /// \returns true if a horizontal reduction was matched and reduced or operands
5562 /// of one of the binary instruction were vectorized.
5563 /// \returns false if a horizontal reduction was not matched (or not possible)
5564 /// or no vectorization of any binary operation feeding \a Root instruction was
5565 /// performed.
5566 static bool tryToVectorizeHorReductionOrInstOperands(
5567     PHINode *P, Instruction *Root, BasicBlock *BB, BoUpSLP &R,
5568     TargetTransformInfo *TTI,
5569     const function_ref<bool(Instruction *, BoUpSLP &)> Vectorize) {
5570   if (!ShouldVectorizeHor)
5571     return false;
5572 
5573   if (!Root)
5574     return false;
5575 
5576   if (Root->getParent() != BB || isa<PHINode>(Root))
5577     return false;
5578   // Start analysis starting from Root instruction. If horizontal reduction is
5579   // found, try to vectorize it. If it is not a horizontal reduction or
5580   // vectorization is not possible or not effective, and currently analyzed
5581   // instruction is a binary operation, try to vectorize the operands, using
5582   // pre-order DFS traversal order. If the operands were not vectorized, repeat
5583   // the same procedure considering each operand as a possible root of the
5584   // horizontal reduction.
5585   // Interrupt the process if the Root instruction itself was vectorized or all
5586   // sub-trees not higher that RecursionMaxDepth were analyzed/vectorized.
5587   SmallVector<std::pair<WeakTrackingVH, unsigned>, 8> Stack(1, {Root, 0});
5588   SmallSet<Value *, 8> VisitedInstrs;
5589   bool Res = false;
5590   while (!Stack.empty()) {
5591     Value *V;
5592     unsigned Level;
5593     std::tie(V, Level) = Stack.pop_back_val();
5594     if (!V)
5595       continue;
5596     auto *Inst = dyn_cast<Instruction>(V);
5597     if (!Inst)
5598       continue;
5599     auto *BI = dyn_cast<BinaryOperator>(Inst);
5600     auto *SI = dyn_cast<SelectInst>(Inst);
5601     if (BI || SI) {
5602       HorizontalReduction HorRdx;
5603       if (HorRdx.matchAssociativeReduction(P, Inst)) {
5604         if (HorRdx.tryToReduce(R, TTI)) {
5605           Res = true;
5606           // Set P to nullptr to avoid re-analysis of phi node in
5607           // matchAssociativeReduction function unless this is the root node.
5608           P = nullptr;
5609           continue;
5610         }
5611       }
5612       if (P && BI) {
5613         Inst = dyn_cast<Instruction>(BI->getOperand(0));
5614         if (Inst == P)
5615           Inst = dyn_cast<Instruction>(BI->getOperand(1));
5616         if (!Inst) {
5617           // Set P to nullptr to avoid re-analysis of phi node in
5618           // matchAssociativeReduction function unless this is the root node.
5619           P = nullptr;
5620           continue;
5621         }
5622       }
5623     }
5624     // Set P to nullptr to avoid re-analysis of phi node in
5625     // matchAssociativeReduction function unless this is the root node.
5626     P = nullptr;
5627     if (Vectorize(Inst, R)) {
5628       Res = true;
5629       continue;
5630     }
5631 
5632     // Try to vectorize operands.
5633     // Continue analysis for the instruction from the same basic block only to
5634     // save compile time.
5635     if (++Level < RecursionMaxDepth)
5636       for (auto *Op : Inst->operand_values())
5637         if (VisitedInstrs.insert(Op).second)
5638           if (auto *I = dyn_cast<Instruction>(Op))
5639             if (!isa<PHINode>(I) && I->getParent() == BB)
5640               Stack.emplace_back(Op, Level);
5641   }
5642   return Res;
5643 }
5644 
5645 bool SLPVectorizerPass::vectorizeRootInstruction(PHINode *P, Value *V,
5646                                                  BasicBlock *BB, BoUpSLP &R,
5647                                                  TargetTransformInfo *TTI) {
5648   if (!V)
5649     return false;
5650   auto *I = dyn_cast<Instruction>(V);
5651   if (!I)
5652     return false;
5653 
5654   if (!isa<BinaryOperator>(I))
5655     P = nullptr;
5656   // Try to match and vectorize a horizontal reduction.
5657   auto &&ExtraVectorization = [this](Instruction *I, BoUpSLP &R) -> bool {
5658     return tryToVectorize(I, R);
5659   };
5660   return tryToVectorizeHorReductionOrInstOperands(P, I, BB, R, TTI,
5661                                                   ExtraVectorization);
5662 }
5663 
5664 bool SLPVectorizerPass::vectorizeInsertValueInst(InsertValueInst *IVI,
5665                                                  BasicBlock *BB, BoUpSLP &R) {
5666   const DataLayout &DL = BB->getModule()->getDataLayout();
5667   if (!R.canMapToVector(IVI->getType(), DL))
5668     return false;
5669 
5670   SmallVector<Value *, 16> BuildVectorOpds;
5671   if (!findBuildAggregate(IVI, BuildVectorOpds))
5672     return false;
5673 
5674   DEBUG(dbgs() << "SLP: array mappable to vector: " << *IVI << "\n");
5675   // Aggregate value is unlikely to be processed in vector register, we need to
5676   // extract scalars into scalar registers, so NeedExtraction is set true.
5677   return tryToVectorizeList(BuildVectorOpds, R);
5678 }
5679 
5680 bool SLPVectorizerPass::vectorizeInsertElementInst(InsertElementInst *IEI,
5681                                                    BasicBlock *BB, BoUpSLP &R) {
5682   SmallVector<Value *, 16> BuildVectorOpds;
5683   if (!findBuildVector(IEI, BuildVectorOpds))
5684     return false;
5685 
5686   // Vectorize starting with the build vector operands ignoring the BuildVector
5687   // instructions for the purpose of scheduling and user extraction.
5688   return tryToVectorizeList(BuildVectorOpds, R);
5689 }
5690 
5691 bool SLPVectorizerPass::vectorizeCmpInst(CmpInst *CI, BasicBlock *BB,
5692                                          BoUpSLP &R) {
5693   if (tryToVectorizePair(CI->getOperand(0), CI->getOperand(1), R))
5694     return true;
5695 
5696   bool OpsChanged = false;
5697   for (int Idx = 0; Idx < 2; ++Idx) {
5698     OpsChanged |=
5699         vectorizeRootInstruction(nullptr, CI->getOperand(Idx), BB, R, TTI);
5700   }
5701   return OpsChanged;
5702 }
5703 
5704 bool SLPVectorizerPass::vectorizeSimpleInstructions(
5705     SmallVectorImpl<WeakVH> &Instructions, BasicBlock *BB, BoUpSLP &R) {
5706   bool OpsChanged = false;
5707   for (auto &VH : reverse(Instructions)) {
5708     auto *I = dyn_cast_or_null<Instruction>(VH);
5709     if (!I)
5710       continue;
5711     if (auto *LastInsertValue = dyn_cast<InsertValueInst>(I))
5712       OpsChanged |= vectorizeInsertValueInst(LastInsertValue, BB, R);
5713     else if (auto *LastInsertElem = dyn_cast<InsertElementInst>(I))
5714       OpsChanged |= vectorizeInsertElementInst(LastInsertElem, BB, R);
5715     else if (auto *CI = dyn_cast<CmpInst>(I))
5716       OpsChanged |= vectorizeCmpInst(CI, BB, R);
5717   }
5718   Instructions.clear();
5719   return OpsChanged;
5720 }
5721 
5722 bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
5723   bool Changed = false;
5724   SmallVector<Value *, 4> Incoming;
5725   SmallSet<Value *, 16> VisitedInstrs;
5726 
5727   bool HaveVectorizedPhiNodes = true;
5728   while (HaveVectorizedPhiNodes) {
5729     HaveVectorizedPhiNodes = false;
5730 
5731     // Collect the incoming values from the PHIs.
5732     Incoming.clear();
5733     for (Instruction &I : *BB) {
5734       PHINode *P = dyn_cast<PHINode>(&I);
5735       if (!P)
5736         break;
5737 
5738       if (!VisitedInstrs.count(P))
5739         Incoming.push_back(P);
5740     }
5741 
5742     // Sort by type.
5743     std::stable_sort(Incoming.begin(), Incoming.end(), PhiTypeSorterFunc);
5744 
5745     // Try to vectorize elements base on their type.
5746     for (SmallVector<Value *, 4>::iterator IncIt = Incoming.begin(),
5747                                            E = Incoming.end();
5748          IncIt != E;) {
5749 
5750       // Look for the next elements with the same type.
5751       SmallVector<Value *, 4>::iterator SameTypeIt = IncIt;
5752       while (SameTypeIt != E &&
5753              (*SameTypeIt)->getType() == (*IncIt)->getType()) {
5754         VisitedInstrs.insert(*SameTypeIt);
5755         ++SameTypeIt;
5756       }
5757 
5758       // Try to vectorize them.
5759       unsigned NumElts = (SameTypeIt - IncIt);
5760       DEBUG(errs() << "SLP: Trying to vectorize starting at PHIs (" << NumElts << ")\n");
5761       // The order in which the phi nodes appear in the program does not matter.
5762       // So allow tryToVectorizeList to reorder them if it is beneficial. This
5763       // is done when there are exactly two elements since tryToVectorizeList
5764       // asserts that there are only two values when AllowReorder is true.
5765       bool AllowReorder = NumElts == 2;
5766       if (NumElts > 1 &&
5767           tryToVectorizeList(makeArrayRef(IncIt, NumElts), R, AllowReorder)) {
5768         // Success start over because instructions might have been changed.
5769         HaveVectorizedPhiNodes = true;
5770         Changed = true;
5771         break;
5772       }
5773 
5774       // Start over at the next instruction of a different type (or the end).
5775       IncIt = SameTypeIt;
5776     }
5777   }
5778 
5779   VisitedInstrs.clear();
5780 
5781   SmallVector<WeakVH, 8> PostProcessInstructions;
5782   SmallDenseSet<Instruction *, 4> KeyNodes;
5783   for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; it++) {
5784     // We may go through BB multiple times so skip the one we have checked.
5785     if (!VisitedInstrs.insert(&*it).second) {
5786       if (it->use_empty() && KeyNodes.count(&*it) > 0 &&
5787           vectorizeSimpleInstructions(PostProcessInstructions, BB, R)) {
5788         // We would like to start over since some instructions are deleted
5789         // and the iterator may become invalid value.
5790         Changed = true;
5791         it = BB->begin();
5792         e = BB->end();
5793       }
5794       continue;
5795     }
5796 
5797     if (isa<DbgInfoIntrinsic>(it))
5798       continue;
5799 
5800     // Try to vectorize reductions that use PHINodes.
5801     if (PHINode *P = dyn_cast<PHINode>(it)) {
5802       // Check that the PHI is a reduction PHI.
5803       if (P->getNumIncomingValues() != 2)
5804         return Changed;
5805 
5806       // Try to match and vectorize a horizontal reduction.
5807       if (vectorizeRootInstruction(P, getReductionValue(DT, P, BB, LI), BB, R,
5808                                    TTI)) {
5809         Changed = true;
5810         it = BB->begin();
5811         e = BB->end();
5812         continue;
5813       }
5814       continue;
5815     }
5816 
5817     // Ran into an instruction without users, like terminator, or function call
5818     // with ignored return value, store. Ignore unused instructions (basing on
5819     // instruction type, except for CallInst and InvokeInst).
5820     if (it->use_empty() && (it->getType()->isVoidTy() || isa<CallInst>(it) ||
5821                             isa<InvokeInst>(it))) {
5822       KeyNodes.insert(&*it);
5823       bool OpsChanged = false;
5824       if (ShouldStartVectorizeHorAtStore || !isa<StoreInst>(it)) {
5825         for (auto *V : it->operand_values()) {
5826           // Try to match and vectorize a horizontal reduction.
5827           OpsChanged |= vectorizeRootInstruction(nullptr, V, BB, R, TTI);
5828         }
5829       }
5830       // Start vectorization of post-process list of instructions from the
5831       // top-tree instructions to try to vectorize as many instructions as
5832       // possible.
5833       OpsChanged |= vectorizeSimpleInstructions(PostProcessInstructions, BB, R);
5834       if (OpsChanged) {
5835         // We would like to start over since some instructions are deleted
5836         // and the iterator may become invalid value.
5837         Changed = true;
5838         it = BB->begin();
5839         e = BB->end();
5840         continue;
5841       }
5842     }
5843 
5844     if (isa<InsertElementInst>(it) || isa<CmpInst>(it) ||
5845         isa<InsertValueInst>(it))
5846       PostProcessInstructions.push_back(&*it);
5847 
5848   }
5849 
5850   return Changed;
5851 }
5852 
5853 bool SLPVectorizerPass::vectorizeGEPIndices(BasicBlock *BB, BoUpSLP &R) {
5854   auto Changed = false;
5855   for (auto &Entry : GEPs) {
5856     // If the getelementptr list has fewer than two elements, there's nothing
5857     // to do.
5858     if (Entry.second.size() < 2)
5859       continue;
5860 
5861     DEBUG(dbgs() << "SLP: Analyzing a getelementptr list of length "
5862                  << Entry.second.size() << ".\n");
5863 
5864     // We process the getelementptr list in chunks of 16 (like we do for
5865     // stores) to minimize compile-time.
5866     for (unsigned BI = 0, BE = Entry.second.size(); BI < BE; BI += 16) {
5867       auto Len = std::min<unsigned>(BE - BI, 16);
5868       auto GEPList = makeArrayRef(&Entry.second[BI], Len);
5869 
5870       // Initialize a set a candidate getelementptrs. Note that we use a
5871       // SetVector here to preserve program order. If the index computations
5872       // are vectorizable and begin with loads, we want to minimize the chance
5873       // of having to reorder them later.
5874       SetVector<Value *> Candidates(GEPList.begin(), GEPList.end());
5875 
5876       // Some of the candidates may have already been vectorized after we
5877       // initially collected them. If so, the WeakTrackingVHs will have
5878       // nullified the
5879       // values, so remove them from the set of candidates.
5880       Candidates.remove(nullptr);
5881 
5882       // Remove from the set of candidates all pairs of getelementptrs with
5883       // constant differences. Such getelementptrs are likely not good
5884       // candidates for vectorization in a bottom-up phase since one can be
5885       // computed from the other. We also ensure all candidate getelementptr
5886       // indices are unique.
5887       for (int I = 0, E = GEPList.size(); I < E && Candidates.size() > 1; ++I) {
5888         auto *GEPI = cast<GetElementPtrInst>(GEPList[I]);
5889         if (!Candidates.count(GEPI))
5890           continue;
5891         auto *SCEVI = SE->getSCEV(GEPList[I]);
5892         for (int J = I + 1; J < E && Candidates.size() > 1; ++J) {
5893           auto *GEPJ = cast<GetElementPtrInst>(GEPList[J]);
5894           auto *SCEVJ = SE->getSCEV(GEPList[J]);
5895           if (isa<SCEVConstant>(SE->getMinusSCEV(SCEVI, SCEVJ))) {
5896             Candidates.remove(GEPList[I]);
5897             Candidates.remove(GEPList[J]);
5898           } else if (GEPI->idx_begin()->get() == GEPJ->idx_begin()->get()) {
5899             Candidates.remove(GEPList[J]);
5900           }
5901         }
5902       }
5903 
5904       // We break out of the above computation as soon as we know there are
5905       // fewer than two candidates remaining.
5906       if (Candidates.size() < 2)
5907         continue;
5908 
5909       // Add the single, non-constant index of each candidate to the bundle. We
5910       // ensured the indices met these constraints when we originally collected
5911       // the getelementptrs.
5912       SmallVector<Value *, 16> Bundle(Candidates.size());
5913       auto BundleIndex = 0u;
5914       for (auto *V : Candidates) {
5915         auto *GEP = cast<GetElementPtrInst>(V);
5916         auto *GEPIdx = GEP->idx_begin()->get();
5917         assert(GEP->getNumIndices() == 1 || !isa<Constant>(GEPIdx));
5918         Bundle[BundleIndex++] = GEPIdx;
5919       }
5920 
5921       // Try and vectorize the indices. We are currently only interested in
5922       // gather-like cases of the form:
5923       //
5924       // ... = g[a[0] - b[0]] + g[a[1] - b[1]] + ...
5925       //
5926       // where the loads of "a", the loads of "b", and the subtractions can be
5927       // performed in parallel. It's likely that detecting this pattern in a
5928       // bottom-up phase will be simpler and less costly than building a
5929       // full-blown top-down phase beginning at the consecutive loads.
5930       Changed |= tryToVectorizeList(Bundle, R);
5931     }
5932   }
5933   return Changed;
5934 }
5935 
5936 bool SLPVectorizerPass::vectorizeStoreChains(BoUpSLP &R) {
5937   bool Changed = false;
5938   // Attempt to sort and vectorize each of the store-groups.
5939   for (StoreListMap::iterator it = Stores.begin(), e = Stores.end(); it != e;
5940        ++it) {
5941     if (it->second.size() < 2)
5942       continue;
5943 
5944     DEBUG(dbgs() << "SLP: Analyzing a store chain of length "
5945           << it->second.size() << ".\n");
5946 
5947     // Process the stores in chunks of 16.
5948     // TODO: The limit of 16 inhibits greater vectorization factors.
5949     //       For example, AVX2 supports v32i8. Increasing this limit, however,
5950     //       may cause a significant compile-time increase.
5951     for (unsigned CI = 0, CE = it->second.size(); CI < CE; CI+=16) {
5952       unsigned Len = std::min<unsigned>(CE - CI, 16);
5953       Changed |= vectorizeStores(makeArrayRef(&it->second[CI], Len), R);
5954     }
5955   }
5956   return Changed;
5957 }
5958 
5959 char SLPVectorizer::ID = 0;
5960 
5961 static const char lv_name[] = "SLP Vectorizer";
5962 
5963 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
5964 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
5965 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
5966 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
5967 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
5968 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
5969 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
5970 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
5971 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
5972 
5973 Pass *llvm::createSLPVectorizerPass() { return new SLPVectorizer(); }
5974