1 //===- SLPVectorizer.cpp - A bottom up SLP Vectorizer ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements the Bottom Up SLP vectorizer. It detects consecutive
10 // stores that can be put together into vector-stores. Next, it attempts to
11 // construct vectorizable tree using the use-def chains. If a profitable tree
12 // was found, the SLP vectorizer performs vectorization on the tree.
13 //
14 // The pass is inspired by the work described in the paper:
15 //  "Loop-Aware SLP in GCC" by Ira Rosen, Dorit Nuzman, Ayal Zaks.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/PriorityQueue.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SetOperations.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/SmallBitVector.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallSet.h"
31 #include "llvm/ADT/SmallString.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/iterator.h"
34 #include "llvm/ADT/iterator_range.h"
35 #include "llvm/Analysis/AliasAnalysis.h"
36 #include "llvm/Analysis/AssumptionCache.h"
37 #include "llvm/Analysis/CodeMetrics.h"
38 #include "llvm/Analysis/DemandedBits.h"
39 #include "llvm/Analysis/GlobalsModRef.h"
40 #include "llvm/Analysis/IVDescriptors.h"
41 #include "llvm/Analysis/LoopAccessAnalysis.h"
42 #include "llvm/Analysis/LoopInfo.h"
43 #include "llvm/Analysis/MemoryLocation.h"
44 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
45 #include "llvm/Analysis/ScalarEvolution.h"
46 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
47 #include "llvm/Analysis/TargetLibraryInfo.h"
48 #include "llvm/Analysis/TargetTransformInfo.h"
49 #include "llvm/Analysis/ValueTracking.h"
50 #include "llvm/Analysis/VectorUtils.h"
51 #include "llvm/IR/Attributes.h"
52 #include "llvm/IR/BasicBlock.h"
53 #include "llvm/IR/Constant.h"
54 #include "llvm/IR/Constants.h"
55 #include "llvm/IR/DataLayout.h"
56 #include "llvm/IR/DebugLoc.h"
57 #include "llvm/IR/DerivedTypes.h"
58 #include "llvm/IR/Dominators.h"
59 #include "llvm/IR/Function.h"
60 #include "llvm/IR/IRBuilder.h"
61 #include "llvm/IR/InstrTypes.h"
62 #include "llvm/IR/Instruction.h"
63 #include "llvm/IR/Instructions.h"
64 #include "llvm/IR/IntrinsicInst.h"
65 #include "llvm/IR/Intrinsics.h"
66 #include "llvm/IR/Module.h"
67 #include "llvm/IR/NoFolder.h"
68 #include "llvm/IR/Operator.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/InitializePasses.h"
77 #include "llvm/Pass.h"
78 #include "llvm/Support/Casting.h"
79 #include "llvm/Support/CommandLine.h"
80 #include "llvm/Support/Compiler.h"
81 #include "llvm/Support/DOTGraphTraits.h"
82 #include "llvm/Support/Debug.h"
83 #include "llvm/Support/ErrorHandling.h"
84 #include "llvm/Support/GraphWriter.h"
85 #include "llvm/Support/InstructionCost.h"
86 #include "llvm/Support/KnownBits.h"
87 #include "llvm/Support/MathExtras.h"
88 #include "llvm/Support/raw_ostream.h"
89 #include "llvm/Transforms/Utils/InjectTLIMappings.h"
90 #include "llvm/Transforms/Utils/LoopUtils.h"
91 #include "llvm/Transforms/Vectorize.h"
92 #include <algorithm>
93 #include <cassert>
94 #include <cstdint>
95 #include <iterator>
96 #include <memory>
97 #include <set>
98 #include <string>
99 #include <tuple>
100 #include <utility>
101 #include <vector>
102 
103 using namespace llvm;
104 using namespace llvm::PatternMatch;
105 using namespace slpvectorizer;
106 
107 #define SV_NAME "slp-vectorizer"
108 #define DEBUG_TYPE "SLP"
109 
110 STATISTIC(NumVectorInstructions, "Number of vector instructions generated");
111 
112 cl::opt<bool> RunSLPVectorization("vectorize-slp", cl::init(true), cl::Hidden,
113                                   cl::desc("Run the SLP vectorization passes"));
114 
115 static cl::opt<int>
116     SLPCostThreshold("slp-threshold", cl::init(0), cl::Hidden,
117                      cl::desc("Only vectorize if you gain more than this "
118                               "number "));
119 
120 static cl::opt<bool>
121 ShouldVectorizeHor("slp-vectorize-hor", cl::init(true), cl::Hidden,
122                    cl::desc("Attempt to vectorize horizontal reductions"));
123 
124 static cl::opt<bool> ShouldStartVectorizeHorAtStore(
125     "slp-vectorize-hor-store", cl::init(false), cl::Hidden,
126     cl::desc(
127         "Attempt to vectorize horizontal reductions feeding into a store"));
128 
129 static cl::opt<int>
130 MaxVectorRegSizeOption("slp-max-reg-size", cl::init(128), cl::Hidden,
131     cl::desc("Attempt to vectorize for this register size in bits"));
132 
133 static cl::opt<unsigned>
134 MaxVFOption("slp-max-vf", cl::init(0), cl::Hidden,
135     cl::desc("Maximum SLP vectorization factor (0=unlimited)"));
136 
137 static cl::opt<int>
138 MaxStoreLookup("slp-max-store-lookup", cl::init(32), cl::Hidden,
139     cl::desc("Maximum depth of the lookup for consecutive stores."));
140 
141 /// Limits the size of scheduling regions in a block.
142 /// It avoid long compile times for _very_ large blocks where vector
143 /// instructions are spread over a wide range.
144 /// This limit is way higher than needed by real-world functions.
145 static cl::opt<int>
146 ScheduleRegionSizeBudget("slp-schedule-budget", cl::init(100000), cl::Hidden,
147     cl::desc("Limit the size of the SLP scheduling region per block"));
148 
149 static cl::opt<int> MinVectorRegSizeOption(
150     "slp-min-reg-size", cl::init(128), cl::Hidden,
151     cl::desc("Attempt to vectorize for this register size in bits"));
152 
153 static cl::opt<unsigned> RecursionMaxDepth(
154     "slp-recursion-max-depth", cl::init(12), cl::Hidden,
155     cl::desc("Limit the recursion depth when building a vectorizable tree"));
156 
157 static cl::opt<unsigned> MinTreeSize(
158     "slp-min-tree-size", cl::init(3), cl::Hidden,
159     cl::desc("Only vectorize small trees if they are fully vectorizable"));
160 
161 // The maximum depth that the look-ahead score heuristic will explore.
162 // The higher this value, the higher the compilation time overhead.
163 static cl::opt<int> LookAheadMaxDepth(
164     "slp-max-look-ahead-depth", cl::init(2), cl::Hidden,
165     cl::desc("The maximum look-ahead depth for operand reordering scores"));
166 
167 // The Look-ahead heuristic goes through the users of the bundle to calculate
168 // the users cost in getExternalUsesCost(). To avoid compilation time increase
169 // we limit the number of users visited to this value.
170 static cl::opt<unsigned> LookAheadUsersBudget(
171     "slp-look-ahead-users-budget", cl::init(2), cl::Hidden,
172     cl::desc("The maximum number of users to visit while visiting the "
173              "predecessors. This prevents compilation time increase."));
174 
175 static cl::opt<bool>
176     ViewSLPTree("view-slp-tree", cl::Hidden,
177                 cl::desc("Display the SLP trees with Graphviz"));
178 
179 // Limit the number of alias checks. The limit is chosen so that
180 // it has no negative effect on the llvm benchmarks.
181 static const unsigned AliasedCheckLimit = 10;
182 
183 // Another limit for the alias checks: The maximum distance between load/store
184 // instructions where alias checks are done.
185 // This limit is useful for very large basic blocks.
186 static const unsigned MaxMemDepDistance = 160;
187 
188 /// If the ScheduleRegionSizeBudget is exhausted, we allow small scheduling
189 /// regions to be handled.
190 static const int MinScheduleRegionSize = 16;
191 
192 /// Predicate for the element types that the SLP vectorizer supports.
193 ///
194 /// The most important thing to filter here are types which are invalid in LLVM
195 /// vectors. We also filter target specific types which have absolutely no
196 /// meaningful vectorization path such as x86_fp80 and ppc_f128. This just
197 /// avoids spending time checking the cost model and realizing that they will
198 /// be inevitably scalarized.
199 static bool isValidElementType(Type *Ty) {
200   return VectorType::isValidElementType(Ty) && !Ty->isX86_FP80Ty() &&
201          !Ty->isPPC_FP128Ty();
202 }
203 
204 /// \returns True if the value is a constant (but not globals/constant
205 /// expressions).
206 static bool isConstant(Value *V) {
207   return isa<Constant>(V) && !isa<ConstantExpr>(V) && !isa<GlobalValue>(V);
208 }
209 
210 /// Checks if \p V is one of vector-like instructions, i.e. undef,
211 /// insertelement/extractelement with constant indices for fixed vector type or
212 /// extractvalue instruction.
213 static bool isVectorLikeInstWithConstOps(Value *V) {
214   if (!isa<InsertElementInst, ExtractElementInst>(V) &&
215       !isa<ExtractValueInst, UndefValue>(V))
216     return false;
217   auto *I = dyn_cast<Instruction>(V);
218   if (!I || isa<ExtractValueInst>(I))
219     return true;
220   if (!isa<FixedVectorType>(I->getOperand(0)->getType()))
221     return false;
222   if (isa<ExtractElementInst>(I))
223     return isConstant(I->getOperand(1));
224   assert(isa<InsertElementInst>(V) && "Expected only insertelement.");
225   return isConstant(I->getOperand(2));
226 }
227 
228 /// \returns true if all of the instructions in \p VL are in the same block or
229 /// false otherwise.
230 static bool allSameBlock(ArrayRef<Value *> VL) {
231   Instruction *I0 = dyn_cast<Instruction>(VL[0]);
232   if (!I0)
233     return false;
234   if (all_of(VL, isVectorLikeInstWithConstOps))
235     return true;
236 
237   BasicBlock *BB = I0->getParent();
238   for (int I = 1, E = VL.size(); I < E; I++) {
239     auto *II = dyn_cast<Instruction>(VL[I]);
240     if (!II)
241       return false;
242 
243     if (BB != II->getParent())
244       return false;
245   }
246   return true;
247 }
248 
249 /// \returns True if all of the values in \p VL are constants (but not
250 /// globals/constant expressions).
251 static bool allConstant(ArrayRef<Value *> VL) {
252   // Constant expressions and globals can't be vectorized like normal integer/FP
253   // constants.
254   return all_of(VL, isConstant);
255 }
256 
257 /// \returns True if all of the values in \p VL are identical or some of them
258 /// are UndefValue.
259 static bool isSplat(ArrayRef<Value *> VL) {
260   Value *FirstNonUndef = nullptr;
261   for (Value *V : VL) {
262     if (isa<UndefValue>(V))
263       continue;
264     if (!FirstNonUndef) {
265       FirstNonUndef = V;
266       continue;
267     }
268     if (V != FirstNonUndef)
269       return false;
270   }
271   return FirstNonUndef != nullptr;
272 }
273 
274 /// \returns True if \p I is commutative, handles CmpInst and BinaryOperator.
275 static bool isCommutative(Instruction *I) {
276   if (auto *Cmp = dyn_cast<CmpInst>(I))
277     return Cmp->isCommutative();
278   if (auto *BO = dyn_cast<BinaryOperator>(I))
279     return BO->isCommutative();
280   // TODO: This should check for generic Instruction::isCommutative(), but
281   //       we need to confirm that the caller code correctly handles Intrinsics
282   //       for example (does not have 2 operands).
283   return false;
284 }
285 
286 /// Checks if the given value is actually an undefined constant vector.
287 static bool isUndefVector(const Value *V) {
288   if (isa<UndefValue>(V))
289     return true;
290   auto *C = dyn_cast<Constant>(V);
291   if (!C)
292     return false;
293   if (!C->containsUndefOrPoisonElement())
294     return false;
295   auto *VecTy = dyn_cast<FixedVectorType>(C->getType());
296   if (!VecTy)
297     return false;
298   for (unsigned I = 0, E = VecTy->getNumElements(); I != E; ++I) {
299     if (Constant *Elem = C->getAggregateElement(I))
300       if (!isa<UndefValue>(Elem))
301         return false;
302   }
303   return true;
304 }
305 
306 /// Checks if the vector of instructions can be represented as a shuffle, like:
307 /// %x0 = extractelement <4 x i8> %x, i32 0
308 /// %x3 = extractelement <4 x i8> %x, i32 3
309 /// %y1 = extractelement <4 x i8> %y, i32 1
310 /// %y2 = extractelement <4 x i8> %y, i32 2
311 /// %x0x0 = mul i8 %x0, %x0
312 /// %x3x3 = mul i8 %x3, %x3
313 /// %y1y1 = mul i8 %y1, %y1
314 /// %y2y2 = mul i8 %y2, %y2
315 /// %ins1 = insertelement <4 x i8> poison, i8 %x0x0, i32 0
316 /// %ins2 = insertelement <4 x i8> %ins1, i8 %x3x3, i32 1
317 /// %ins3 = insertelement <4 x i8> %ins2, i8 %y1y1, i32 2
318 /// %ins4 = insertelement <4 x i8> %ins3, i8 %y2y2, i32 3
319 /// ret <4 x i8> %ins4
320 /// can be transformed into:
321 /// %1 = shufflevector <4 x i8> %x, <4 x i8> %y, <4 x i32> <i32 0, i32 3, i32 5,
322 ///                                                         i32 6>
323 /// %2 = mul <4 x i8> %1, %1
324 /// ret <4 x i8> %2
325 /// We convert this initially to something like:
326 /// %x0 = extractelement <4 x i8> %x, i32 0
327 /// %x3 = extractelement <4 x i8> %x, i32 3
328 /// %y1 = extractelement <4 x i8> %y, i32 1
329 /// %y2 = extractelement <4 x i8> %y, i32 2
330 /// %1 = insertelement <4 x i8> poison, i8 %x0, i32 0
331 /// %2 = insertelement <4 x i8> %1, i8 %x3, i32 1
332 /// %3 = insertelement <4 x i8> %2, i8 %y1, i32 2
333 /// %4 = insertelement <4 x i8> %3, i8 %y2, i32 3
334 /// %5 = mul <4 x i8> %4, %4
335 /// %6 = extractelement <4 x i8> %5, i32 0
336 /// %ins1 = insertelement <4 x i8> poison, i8 %6, i32 0
337 /// %7 = extractelement <4 x i8> %5, i32 1
338 /// %ins2 = insertelement <4 x i8> %ins1, i8 %7, i32 1
339 /// %8 = extractelement <4 x i8> %5, i32 2
340 /// %ins3 = insertelement <4 x i8> %ins2, i8 %8, i32 2
341 /// %9 = extractelement <4 x i8> %5, i32 3
342 /// %ins4 = insertelement <4 x i8> %ins3, i8 %9, i32 3
343 /// ret <4 x i8> %ins4
344 /// InstCombiner transforms this into a shuffle and vector mul
345 /// Mask will return the Shuffle Mask equivalent to the extracted elements.
346 /// TODO: Can we split off and reuse the shuffle mask detection from
347 /// TargetTransformInfo::getInstructionThroughput?
348 static Optional<TargetTransformInfo::ShuffleKind>
349 isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
350   const auto *It =
351       find_if(VL, [](Value *V) { return isa<ExtractElementInst>(V); });
352   if (It == VL.end())
353     return None;
354   auto *EI0 = cast<ExtractElementInst>(*It);
355   if (isa<ScalableVectorType>(EI0->getVectorOperandType()))
356     return None;
357   unsigned Size =
358       cast<FixedVectorType>(EI0->getVectorOperandType())->getNumElements();
359   Value *Vec1 = nullptr;
360   Value *Vec2 = nullptr;
361   enum ShuffleMode { Unknown, Select, Permute };
362   ShuffleMode CommonShuffleMode = Unknown;
363   Mask.assign(VL.size(), UndefMaskElem);
364   for (unsigned I = 0, E = VL.size(); I < E; ++I) {
365     // Undef can be represented as an undef element in a vector.
366     if (isa<UndefValue>(VL[I]))
367       continue;
368     auto *EI = cast<ExtractElementInst>(VL[I]);
369     if (isa<ScalableVectorType>(EI->getVectorOperandType()))
370       return None;
371     auto *Vec = EI->getVectorOperand();
372     // We can extractelement from undef or poison vector.
373     if (isUndefVector(Vec))
374       continue;
375     // All vector operands must have the same number of vector elements.
376     if (cast<FixedVectorType>(Vec->getType())->getNumElements() != Size)
377       return None;
378     if (isa<UndefValue>(EI->getIndexOperand()))
379       continue;
380     auto *Idx = dyn_cast<ConstantInt>(EI->getIndexOperand());
381     if (!Idx)
382       return None;
383     // Undefined behavior if Idx is negative or >= Size.
384     if (Idx->getValue().uge(Size))
385       continue;
386     unsigned IntIdx = Idx->getValue().getZExtValue();
387     Mask[I] = IntIdx;
388     // For correct shuffling we have to have at most 2 different vector operands
389     // in all extractelement instructions.
390     if (!Vec1 || Vec1 == Vec) {
391       Vec1 = Vec;
392     } else if (!Vec2 || Vec2 == Vec) {
393       Vec2 = Vec;
394       Mask[I] += Size;
395     } else {
396       return None;
397     }
398     if (CommonShuffleMode == Permute)
399       continue;
400     // If the extract index is not the same as the operation number, it is a
401     // permutation.
402     if (IntIdx != I) {
403       CommonShuffleMode = Permute;
404       continue;
405     }
406     CommonShuffleMode = Select;
407   }
408   // If we're not crossing lanes in different vectors, consider it as blending.
409   if (CommonShuffleMode == Select && Vec2)
410     return TargetTransformInfo::SK_Select;
411   // If Vec2 was never used, we have a permutation of a single vector, otherwise
412   // we have permutation of 2 vectors.
413   return Vec2 ? TargetTransformInfo::SK_PermuteTwoSrc
414               : TargetTransformInfo::SK_PermuteSingleSrc;
415 }
416 
417 namespace {
418 
419 /// Main data required for vectorization of instructions.
420 struct InstructionsState {
421   /// The very first instruction in the list with the main opcode.
422   Value *OpValue = nullptr;
423 
424   /// The main/alternate instruction.
425   Instruction *MainOp = nullptr;
426   Instruction *AltOp = nullptr;
427 
428   /// The main/alternate opcodes for the list of instructions.
429   unsigned getOpcode() const {
430     return MainOp ? MainOp->getOpcode() : 0;
431   }
432 
433   unsigned getAltOpcode() const {
434     return AltOp ? AltOp->getOpcode() : 0;
435   }
436 
437   /// Some of the instructions in the list have alternate opcodes.
438   bool isAltShuffle() const { return AltOp != MainOp; }
439 
440   bool isOpcodeOrAlt(Instruction *I) const {
441     unsigned CheckedOpcode = I->getOpcode();
442     return getOpcode() == CheckedOpcode || getAltOpcode() == CheckedOpcode;
443   }
444 
445   InstructionsState() = delete;
446   InstructionsState(Value *OpValue, Instruction *MainOp, Instruction *AltOp)
447       : OpValue(OpValue), MainOp(MainOp), AltOp(AltOp) {}
448 };
449 
450 } // end anonymous namespace
451 
452 /// Chooses the correct key for scheduling data. If \p Op has the same (or
453 /// alternate) opcode as \p OpValue, the key is \p Op. Otherwise the key is \p
454 /// OpValue.
455 static Value *isOneOf(const InstructionsState &S, Value *Op) {
456   auto *I = dyn_cast<Instruction>(Op);
457   if (I && S.isOpcodeOrAlt(I))
458     return Op;
459   return S.OpValue;
460 }
461 
462 /// \returns true if \p Opcode is allowed as part of of the main/alternate
463 /// instruction for SLP vectorization.
464 ///
465 /// Example of unsupported opcode is SDIV that can potentially cause UB if the
466 /// "shuffled out" lane would result in division by zero.
467 static bool isValidForAlternation(unsigned Opcode) {
468   if (Instruction::isIntDivRem(Opcode))
469     return false;
470 
471   return true;
472 }
473 
474 /// \returns analysis of the Instructions in \p VL described in
475 /// InstructionsState, the Opcode that we suppose the whole list
476 /// could be vectorized even if its structure is diverse.
477 static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
478                                        unsigned BaseIndex = 0) {
479   // Make sure these are all Instructions.
480   if (llvm::any_of(VL, [](Value *V) { return !isa<Instruction>(V); }))
481     return InstructionsState(VL[BaseIndex], nullptr, nullptr);
482 
483   bool IsCastOp = isa<CastInst>(VL[BaseIndex]);
484   bool IsBinOp = isa<BinaryOperator>(VL[BaseIndex]);
485   unsigned Opcode = cast<Instruction>(VL[BaseIndex])->getOpcode();
486   unsigned AltOpcode = Opcode;
487   unsigned AltIndex = BaseIndex;
488 
489   // Check for one alternate opcode from another BinaryOperator.
490   // TODO - generalize to support all operators (types, calls etc.).
491   for (int Cnt = 0, E = VL.size(); Cnt < E; Cnt++) {
492     unsigned InstOpcode = cast<Instruction>(VL[Cnt])->getOpcode();
493     if (IsBinOp && isa<BinaryOperator>(VL[Cnt])) {
494       if (InstOpcode == Opcode || InstOpcode == AltOpcode)
495         continue;
496       if (Opcode == AltOpcode && isValidForAlternation(InstOpcode) &&
497           isValidForAlternation(Opcode)) {
498         AltOpcode = InstOpcode;
499         AltIndex = Cnt;
500         continue;
501       }
502     } else if (IsCastOp && isa<CastInst>(VL[Cnt])) {
503       Type *Ty0 = cast<Instruction>(VL[BaseIndex])->getOperand(0)->getType();
504       Type *Ty1 = cast<Instruction>(VL[Cnt])->getOperand(0)->getType();
505       if (Ty0 == Ty1) {
506         if (InstOpcode == Opcode || InstOpcode == AltOpcode)
507           continue;
508         if (Opcode == AltOpcode) {
509           assert(isValidForAlternation(Opcode) &&
510                  isValidForAlternation(InstOpcode) &&
511                  "Cast isn't safe for alternation, logic needs to be updated!");
512           AltOpcode = InstOpcode;
513           AltIndex = Cnt;
514           continue;
515         }
516       }
517     } else if (InstOpcode == Opcode || InstOpcode == AltOpcode)
518       continue;
519     return InstructionsState(VL[BaseIndex], nullptr, nullptr);
520   }
521 
522   return InstructionsState(VL[BaseIndex], cast<Instruction>(VL[BaseIndex]),
523                            cast<Instruction>(VL[AltIndex]));
524 }
525 
526 /// \returns true if all of the values in \p VL have the same type or false
527 /// otherwise.
528 static bool allSameType(ArrayRef<Value *> VL) {
529   Type *Ty = VL[0]->getType();
530   for (int i = 1, e = VL.size(); i < e; i++)
531     if (VL[i]->getType() != Ty)
532       return false;
533 
534   return true;
535 }
536 
537 /// \returns True if Extract{Value,Element} instruction extracts element Idx.
538 static Optional<unsigned> getExtractIndex(Instruction *E) {
539   unsigned Opcode = E->getOpcode();
540   assert((Opcode == Instruction::ExtractElement ||
541           Opcode == Instruction::ExtractValue) &&
542          "Expected extractelement or extractvalue instruction.");
543   if (Opcode == Instruction::ExtractElement) {
544     auto *CI = dyn_cast<ConstantInt>(E->getOperand(1));
545     if (!CI)
546       return None;
547     return CI->getZExtValue();
548   }
549   ExtractValueInst *EI = cast<ExtractValueInst>(E);
550   if (EI->getNumIndices() != 1)
551     return None;
552   return *EI->idx_begin();
553 }
554 
555 /// \returns True if in-tree use also needs extract. This refers to
556 /// possible scalar operand in vectorized instruction.
557 static bool InTreeUserNeedToExtract(Value *Scalar, Instruction *UserInst,
558                                     TargetLibraryInfo *TLI) {
559   unsigned Opcode = UserInst->getOpcode();
560   switch (Opcode) {
561   case Instruction::Load: {
562     LoadInst *LI = cast<LoadInst>(UserInst);
563     return (LI->getPointerOperand() == Scalar);
564   }
565   case Instruction::Store: {
566     StoreInst *SI = cast<StoreInst>(UserInst);
567     return (SI->getPointerOperand() == Scalar);
568   }
569   case Instruction::Call: {
570     CallInst *CI = cast<CallInst>(UserInst);
571     Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
572     for (unsigned i = 0, e = CI->arg_size(); i != e; ++i) {
573       if (hasVectorInstrinsicScalarOpd(ID, i))
574         return (CI->getArgOperand(i) == Scalar);
575     }
576     LLVM_FALLTHROUGH;
577   }
578   default:
579     return false;
580   }
581 }
582 
583 /// \returns the AA location that is being access by the instruction.
584 static MemoryLocation getLocation(Instruction *I, AAResults *AA) {
585   if (StoreInst *SI = dyn_cast<StoreInst>(I))
586     return MemoryLocation::get(SI);
587   if (LoadInst *LI = dyn_cast<LoadInst>(I))
588     return MemoryLocation::get(LI);
589   return MemoryLocation();
590 }
591 
592 /// \returns True if the instruction is not a volatile or atomic load/store.
593 static bool isSimple(Instruction *I) {
594   if (LoadInst *LI = dyn_cast<LoadInst>(I))
595     return LI->isSimple();
596   if (StoreInst *SI = dyn_cast<StoreInst>(I))
597     return SI->isSimple();
598   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
599     return !MI->isVolatile();
600   return true;
601 }
602 
603 /// Shuffles \p Mask in accordance with the given \p SubMask.
604 static void addMask(SmallVectorImpl<int> &Mask, ArrayRef<int> SubMask) {
605   if (SubMask.empty())
606     return;
607   if (Mask.empty()) {
608     Mask.append(SubMask.begin(), SubMask.end());
609     return;
610   }
611   SmallVector<int> NewMask(SubMask.size(), UndefMaskElem);
612   int TermValue = std::min(Mask.size(), SubMask.size());
613   for (int I = 0, E = SubMask.size(); I < E; ++I) {
614     if (SubMask[I] >= TermValue || SubMask[I] == UndefMaskElem ||
615         Mask[SubMask[I]] >= TermValue)
616       continue;
617     NewMask[I] = Mask[SubMask[I]];
618   }
619   Mask.swap(NewMask);
620 }
621 
622 /// Order may have elements assigned special value (size) which is out of
623 /// bounds. Such indices only appear on places which correspond to undef values
624 /// (see canReuseExtract for details) and used in order to avoid undef values
625 /// have effect on operands ordering.
626 /// The first loop below simply finds all unused indices and then the next loop
627 /// nest assigns these indices for undef values positions.
628 /// As an example below Order has two undef positions and they have assigned
629 /// values 3 and 7 respectively:
630 /// before:  6 9 5 4 9 2 1 0
631 /// after:   6 3 5 4 7 2 1 0
632 static void fixupOrderingIndices(SmallVectorImpl<unsigned> &Order) {
633   const unsigned Sz = Order.size();
634   SmallBitVector UnusedIndices(Sz, /*t=*/true);
635   SmallBitVector MaskedIndices(Sz);
636   for (unsigned I = 0; I < Sz; ++I) {
637     if (Order[I] < Sz)
638       UnusedIndices.reset(Order[I]);
639     else
640       MaskedIndices.set(I);
641   }
642   if (MaskedIndices.none())
643     return;
644   assert(UnusedIndices.count() == MaskedIndices.count() &&
645          "Non-synced masked/available indices.");
646   int Idx = UnusedIndices.find_first();
647   int MIdx = MaskedIndices.find_first();
648   while (MIdx >= 0) {
649     assert(Idx >= 0 && "Indices must be synced.");
650     Order[MIdx] = Idx;
651     Idx = UnusedIndices.find_next(Idx);
652     MIdx = MaskedIndices.find_next(MIdx);
653   }
654 }
655 
656 namespace llvm {
657 
658 static void inversePermutation(ArrayRef<unsigned> Indices,
659                                SmallVectorImpl<int> &Mask) {
660   Mask.clear();
661   const unsigned E = Indices.size();
662   Mask.resize(E, UndefMaskElem);
663   for (unsigned I = 0; I < E; ++I)
664     Mask[Indices[I]] = I;
665 }
666 
667 /// \returns inserting index of InsertElement or InsertValue instruction,
668 /// using Offset as base offset for index.
669 static Optional<int> getInsertIndex(Value *InsertInst, unsigned Offset) {
670   int Index = Offset;
671   if (auto *IE = dyn_cast<InsertElementInst>(InsertInst)) {
672     if (auto *CI = dyn_cast<ConstantInt>(IE->getOperand(2))) {
673       auto *VT = cast<FixedVectorType>(IE->getType());
674       if (CI->getValue().uge(VT->getNumElements()))
675         return UndefMaskElem;
676       Index *= VT->getNumElements();
677       Index += CI->getZExtValue();
678       return Index;
679     }
680     if (isa<UndefValue>(IE->getOperand(2)))
681       return UndefMaskElem;
682     return None;
683   }
684 
685   auto *IV = cast<InsertValueInst>(InsertInst);
686   Type *CurrentType = IV->getType();
687   for (unsigned I : IV->indices()) {
688     if (auto *ST = dyn_cast<StructType>(CurrentType)) {
689       Index *= ST->getNumElements();
690       CurrentType = ST->getElementType(I);
691     } else if (auto *AT = dyn_cast<ArrayType>(CurrentType)) {
692       Index *= AT->getNumElements();
693       CurrentType = AT->getElementType();
694     } else {
695       return None;
696     }
697     Index += I;
698   }
699   return Index;
700 }
701 
702 /// Reorders the list of scalars in accordance with the given \p Order and then
703 /// the \p Mask. \p Order - is the original order of the scalars, need to
704 /// reorder scalars into an unordered state at first according to the given
705 /// order. Then the ordered scalars are shuffled once again in accordance with
706 /// the provided mask.
707 static void reorderScalars(SmallVectorImpl<Value *> &Scalars,
708                            ArrayRef<int> Mask) {
709   assert(!Mask.empty() && "Expected non-empty mask.");
710   SmallVector<Value *> Prev(Scalars.size(),
711                             UndefValue::get(Scalars.front()->getType()));
712   Prev.swap(Scalars);
713   for (unsigned I = 0, E = Prev.size(); I < E; ++I)
714     if (Mask[I] != UndefMaskElem)
715       Scalars[Mask[I]] = Prev[I];
716 }
717 
718 namespace slpvectorizer {
719 
720 /// Bottom Up SLP Vectorizer.
721 class BoUpSLP {
722   struct TreeEntry;
723   struct ScheduleData;
724 
725 public:
726   using ValueList = SmallVector<Value *, 8>;
727   using InstrList = SmallVector<Instruction *, 16>;
728   using ValueSet = SmallPtrSet<Value *, 16>;
729   using StoreList = SmallVector<StoreInst *, 8>;
730   using ExtraValueToDebugLocsMap =
731       MapVector<Value *, SmallVector<Instruction *, 2>>;
732   using OrdersType = SmallVector<unsigned, 4>;
733 
734   BoUpSLP(Function *Func, ScalarEvolution *Se, TargetTransformInfo *Tti,
735           TargetLibraryInfo *TLi, AAResults *Aa, LoopInfo *Li,
736           DominatorTree *Dt, AssumptionCache *AC, DemandedBits *DB,
737           const DataLayout *DL, OptimizationRemarkEmitter *ORE)
738       : F(Func), SE(Se), TTI(Tti), TLI(TLi), AA(Aa), LI(Li), DT(Dt), AC(AC),
739         DB(DB), DL(DL), ORE(ORE), Builder(Se->getContext()) {
740     CodeMetrics::collectEphemeralValues(F, AC, EphValues);
741     // Use the vector register size specified by the target unless overridden
742     // by a command-line option.
743     // TODO: It would be better to limit the vectorization factor based on
744     //       data type rather than just register size. For example, x86 AVX has
745     //       256-bit registers, but it does not support integer operations
746     //       at that width (that requires AVX2).
747     if (MaxVectorRegSizeOption.getNumOccurrences())
748       MaxVecRegSize = MaxVectorRegSizeOption;
749     else
750       MaxVecRegSize =
751           TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector)
752               .getFixedSize();
753 
754     if (MinVectorRegSizeOption.getNumOccurrences())
755       MinVecRegSize = MinVectorRegSizeOption;
756     else
757       MinVecRegSize = TTI->getMinVectorRegisterBitWidth();
758   }
759 
760   /// Vectorize the tree that starts with the elements in \p VL.
761   /// Returns the vectorized root.
762   Value *vectorizeTree();
763 
764   /// Vectorize the tree but with the list of externally used values \p
765   /// ExternallyUsedValues. Values in this MapVector can be replaced but the
766   /// generated extractvalue instructions.
767   Value *vectorizeTree(ExtraValueToDebugLocsMap &ExternallyUsedValues);
768 
769   /// \returns the cost incurred by unwanted spills and fills, caused by
770   /// holding live values over call sites.
771   InstructionCost getSpillCost() const;
772 
773   /// \returns the vectorization cost of the subtree that starts at \p VL.
774   /// A negative number means that this is profitable.
775   InstructionCost getTreeCost(ArrayRef<Value *> VectorizedVals = None);
776 
777   /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
778   /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
779   void buildTree(ArrayRef<Value *> Roots,
780                  ArrayRef<Value *> UserIgnoreLst = None);
781 
782   /// Builds external uses of the vectorized scalars, i.e. the list of
783   /// vectorized scalars to be extracted, their lanes and their scalar users. \p
784   /// ExternallyUsedValues contains additional list of external uses to handle
785   /// vectorization of reductions.
786   void
787   buildExternalUses(const ExtraValueToDebugLocsMap &ExternallyUsedValues = {});
788 
789   /// Clear the internal data structures that are created by 'buildTree'.
790   void deleteTree() {
791     VectorizableTree.clear();
792     ScalarToTreeEntry.clear();
793     MustGather.clear();
794     ExternalUses.clear();
795     for (auto &Iter : BlocksSchedules) {
796       BlockScheduling *BS = Iter.second.get();
797       BS->clear();
798     }
799     MinBWs.clear();
800     InstrElementSize.clear();
801   }
802 
803   unsigned getTreeSize() const { return VectorizableTree.size(); }
804 
805   /// Perform LICM and CSE on the newly generated gather sequences.
806   void optimizeGatherSequence();
807 
808   /// Checks if the specified gather tree entry \p TE can be represented as a
809   /// shuffled vector entry + (possibly) permutation with other gathers. It
810   /// implements the checks only for possibly ordered scalars (Loads,
811   /// ExtractElement, ExtractValue), which can be part of the graph.
812   Optional<OrdersType> findReusedOrderedScalars(const TreeEntry &TE);
813 
814   /// Gets reordering data for the given tree entry. If the entry is vectorized
815   /// - just return ReorderIndices, otherwise check if the scalars can be
816   /// reordered and return the most optimal order.
817   /// \param TopToBottom If true, include the order of vectorized stores and
818   /// insertelement nodes, otherwise skip them.
819   Optional<OrdersType> getReorderingData(const TreeEntry &TE, bool TopToBottom);
820 
821   /// Reorders the current graph to the most profitable order starting from the
822   /// root node to the leaf nodes. The best order is chosen only from the nodes
823   /// of the same size (vectorization factor). Smaller nodes are considered
824   /// parts of subgraph with smaller VF and they are reordered independently. We
825   /// can make it because we still need to extend smaller nodes to the wider VF
826   /// and we can merge reordering shuffles with the widening shuffles.
827   void reorderTopToBottom();
828 
829   /// Reorders the current graph to the most profitable order starting from
830   /// leaves to the root. It allows to rotate small subgraphs and reduce the
831   /// number of reshuffles if the leaf nodes use the same order. In this case we
832   /// can merge the orders and just shuffle user node instead of shuffling its
833   /// operands. Plus, even the leaf nodes have different orders, it allows to
834   /// sink reordering in the graph closer to the root node and merge it later
835   /// during analysis.
836   void reorderBottomToTop(bool IgnoreReorder = false);
837 
838   /// \return The vector element size in bits to use when vectorizing the
839   /// expression tree ending at \p V. If V is a store, the size is the width of
840   /// the stored value. Otherwise, the size is the width of the largest loaded
841   /// value reaching V. This method is used by the vectorizer to calculate
842   /// vectorization factors.
843   unsigned getVectorElementSize(Value *V);
844 
845   /// Compute the minimum type sizes required to represent the entries in a
846   /// vectorizable tree.
847   void computeMinimumValueSizes();
848 
849   // \returns maximum vector register size as set by TTI or overridden by cl::opt.
850   unsigned getMaxVecRegSize() const {
851     return MaxVecRegSize;
852   }
853 
854   // \returns minimum vector register size as set by cl::opt.
855   unsigned getMinVecRegSize() const {
856     return MinVecRegSize;
857   }
858 
859   unsigned getMinVF(unsigned Sz) const {
860     return std::max(2U, getMinVecRegSize() / Sz);
861   }
862 
863   unsigned getMaximumVF(unsigned ElemWidth, unsigned Opcode) const {
864     unsigned MaxVF = MaxVFOption.getNumOccurrences() ?
865       MaxVFOption : TTI->getMaximumVF(ElemWidth, Opcode);
866     return MaxVF ? MaxVF : UINT_MAX;
867   }
868 
869   /// Check if homogeneous aggregate is isomorphic to some VectorType.
870   /// Accepts homogeneous multidimensional aggregate of scalars/vectors like
871   /// {[4 x i16], [4 x i16]}, { <2 x float>, <2 x float> },
872   /// {{{i16, i16}, {i16, i16}}, {{i16, i16}, {i16, i16}}} and so on.
873   ///
874   /// \returns number of elements in vector if isomorphism exists, 0 otherwise.
875   unsigned canMapToVector(Type *T, const DataLayout &DL) const;
876 
877   /// \returns True if the VectorizableTree is both tiny and not fully
878   /// vectorizable. We do not vectorize such trees.
879   bool isTreeTinyAndNotFullyVectorizable(bool ForReduction = false) const;
880 
881   /// Assume that a legal-sized 'or'-reduction of shifted/zexted loaded values
882   /// can be load combined in the backend. Load combining may not be allowed in
883   /// the IR optimizer, so we do not want to alter the pattern. For example,
884   /// partially transforming a scalar bswap() pattern into vector code is
885   /// effectively impossible for the backend to undo.
886   /// TODO: If load combining is allowed in the IR optimizer, this analysis
887   ///       may not be necessary.
888   bool isLoadCombineReductionCandidate(RecurKind RdxKind) const;
889 
890   /// Assume that a vector of stores of bitwise-or/shifted/zexted loaded values
891   /// can be load combined in the backend. Load combining may not be allowed in
892   /// the IR optimizer, so we do not want to alter the pattern. For example,
893   /// partially transforming a scalar bswap() pattern into vector code is
894   /// effectively impossible for the backend to undo.
895   /// TODO: If load combining is allowed in the IR optimizer, this analysis
896   ///       may not be necessary.
897   bool isLoadCombineCandidate() const;
898 
899   OptimizationRemarkEmitter *getORE() { return ORE; }
900 
901   /// This structure holds any data we need about the edges being traversed
902   /// during buildTree_rec(). We keep track of:
903   /// (i) the user TreeEntry index, and
904   /// (ii) the index of the edge.
905   struct EdgeInfo {
906     EdgeInfo() = default;
907     EdgeInfo(TreeEntry *UserTE, unsigned EdgeIdx)
908         : UserTE(UserTE), EdgeIdx(EdgeIdx) {}
909     /// The user TreeEntry.
910     TreeEntry *UserTE = nullptr;
911     /// The operand index of the use.
912     unsigned EdgeIdx = UINT_MAX;
913 #ifndef NDEBUG
914     friend inline raw_ostream &operator<<(raw_ostream &OS,
915                                           const BoUpSLP::EdgeInfo &EI) {
916       EI.dump(OS);
917       return OS;
918     }
919     /// Debug print.
920     void dump(raw_ostream &OS) const {
921       OS << "{User:" << (UserTE ? std::to_string(UserTE->Idx) : "null")
922          << " EdgeIdx:" << EdgeIdx << "}";
923     }
924     LLVM_DUMP_METHOD void dump() const { dump(dbgs()); }
925 #endif
926   };
927 
928   /// A helper data structure to hold the operands of a vector of instructions.
929   /// This supports a fixed vector length for all operand vectors.
930   class VLOperands {
931     /// For each operand we need (i) the value, and (ii) the opcode that it
932     /// would be attached to if the expression was in a left-linearized form.
933     /// This is required to avoid illegal operand reordering.
934     /// For example:
935     /// \verbatim
936     ///                         0 Op1
937     ///                         |/
938     /// Op1 Op2   Linearized    + Op2
939     ///   \ /     ---------->   |/
940     ///    -                    -
941     ///
942     /// Op1 - Op2            (0 + Op1) - Op2
943     /// \endverbatim
944     ///
945     /// Value Op1 is attached to a '+' operation, and Op2 to a '-'.
946     ///
947     /// Another way to think of this is to track all the operations across the
948     /// path from the operand all the way to the root of the tree and to
949     /// calculate the operation that corresponds to this path. For example, the
950     /// path from Op2 to the root crosses the RHS of the '-', therefore the
951     /// corresponding operation is a '-' (which matches the one in the
952     /// linearized tree, as shown above).
953     ///
954     /// For lack of a better term, we refer to this operation as Accumulated
955     /// Path Operation (APO).
956     struct OperandData {
957       OperandData() = default;
958       OperandData(Value *V, bool APO, bool IsUsed)
959           : V(V), APO(APO), IsUsed(IsUsed) {}
960       /// The operand value.
961       Value *V = nullptr;
962       /// TreeEntries only allow a single opcode, or an alternate sequence of
963       /// them (e.g, +, -). Therefore, we can safely use a boolean value for the
964       /// APO. It is set to 'true' if 'V' is attached to an inverse operation
965       /// in the left-linearized form (e.g., Sub/Div), and 'false' otherwise
966       /// (e.g., Add/Mul)
967       bool APO = false;
968       /// Helper data for the reordering function.
969       bool IsUsed = false;
970     };
971 
972     /// During operand reordering, we are trying to select the operand at lane
973     /// that matches best with the operand at the neighboring lane. Our
974     /// selection is based on the type of value we are looking for. For example,
975     /// if the neighboring lane has a load, we need to look for a load that is
976     /// accessing a consecutive address. These strategies are summarized in the
977     /// 'ReorderingMode' enumerator.
978     enum class ReorderingMode {
979       Load,     ///< Matching loads to consecutive memory addresses
980       Opcode,   ///< Matching instructions based on opcode (same or alternate)
981       Constant, ///< Matching constants
982       Splat,    ///< Matching the same instruction multiple times (broadcast)
983       Failed,   ///< We failed to create a vectorizable group
984     };
985 
986     using OperandDataVec = SmallVector<OperandData, 2>;
987 
988     /// A vector of operand vectors.
989     SmallVector<OperandDataVec, 4> OpsVec;
990 
991     const DataLayout &DL;
992     ScalarEvolution &SE;
993     const BoUpSLP &R;
994 
995     /// \returns the operand data at \p OpIdx and \p Lane.
996     OperandData &getData(unsigned OpIdx, unsigned Lane) {
997       return OpsVec[OpIdx][Lane];
998     }
999 
1000     /// \returns the operand data at \p OpIdx and \p Lane. Const version.
1001     const OperandData &getData(unsigned OpIdx, unsigned Lane) const {
1002       return OpsVec[OpIdx][Lane];
1003     }
1004 
1005     /// Clears the used flag for all entries.
1006     void clearUsed() {
1007       for (unsigned OpIdx = 0, NumOperands = getNumOperands();
1008            OpIdx != NumOperands; ++OpIdx)
1009         for (unsigned Lane = 0, NumLanes = getNumLanes(); Lane != NumLanes;
1010              ++Lane)
1011           OpsVec[OpIdx][Lane].IsUsed = false;
1012     }
1013 
1014     /// Swap the operand at \p OpIdx1 with that one at \p OpIdx2.
1015     void swap(unsigned OpIdx1, unsigned OpIdx2, unsigned Lane) {
1016       std::swap(OpsVec[OpIdx1][Lane], OpsVec[OpIdx2][Lane]);
1017     }
1018 
1019     // The hard-coded scores listed here are not very important, though it shall
1020     // be higher for better matches to improve the resulting cost. When
1021     // computing the scores of matching one sub-tree with another, we are
1022     // basically counting the number of values that are matching. So even if all
1023     // scores are set to 1, we would still get a decent matching result.
1024     // However, sometimes we have to break ties. For example we may have to
1025     // choose between matching loads vs matching opcodes. This is what these
1026     // scores are helping us with: they provide the order of preference. Also,
1027     // this is important if the scalar is externally used or used in another
1028     // tree entry node in the different lane.
1029 
1030     /// Loads from consecutive memory addresses, e.g. load(A[i]), load(A[i+1]).
1031     static const int ScoreConsecutiveLoads = 4;
1032     /// Loads from reversed memory addresses, e.g. load(A[i+1]), load(A[i]).
1033     static const int ScoreReversedLoads = 3;
1034     /// ExtractElementInst from same vector and consecutive indexes.
1035     static const int ScoreConsecutiveExtracts = 4;
1036     /// ExtractElementInst from same vector and reversed indices.
1037     static const int ScoreReversedExtracts = 3;
1038     /// Constants.
1039     static const int ScoreConstants = 2;
1040     /// Instructions with the same opcode.
1041     static const int ScoreSameOpcode = 2;
1042     /// Instructions with alt opcodes (e.g, add + sub).
1043     static const int ScoreAltOpcodes = 1;
1044     /// Identical instructions (a.k.a. splat or broadcast).
1045     static const int ScoreSplat = 1;
1046     /// Matching with an undef is preferable to failing.
1047     static const int ScoreUndef = 1;
1048     /// Score for failing to find a decent match.
1049     static const int ScoreFail = 0;
1050     /// User exteranl to the vectorized code.
1051     static const int ExternalUseCost = 1;
1052     /// The user is internal but in a different lane.
1053     static const int UserInDiffLaneCost = ExternalUseCost;
1054 
1055     /// \returns the score of placing \p V1 and \p V2 in consecutive lanes.
1056     static int getShallowScore(Value *V1, Value *V2, const DataLayout &DL,
1057                                ScalarEvolution &SE, int NumLanes) {
1058       if (V1 == V2)
1059         return VLOperands::ScoreSplat;
1060 
1061       auto *LI1 = dyn_cast<LoadInst>(V1);
1062       auto *LI2 = dyn_cast<LoadInst>(V2);
1063       if (LI1 && LI2) {
1064         if (LI1->getParent() != LI2->getParent())
1065           return VLOperands::ScoreFail;
1066 
1067         Optional<int> Dist = getPointersDiff(
1068             LI1->getType(), LI1->getPointerOperand(), LI2->getType(),
1069             LI2->getPointerOperand(), DL, SE, /*StrictCheck=*/true);
1070         if (!Dist)
1071           return VLOperands::ScoreFail;
1072         // The distance is too large - still may be profitable to use masked
1073         // loads/gathers.
1074         if (std::abs(*Dist) > NumLanes / 2)
1075           return VLOperands::ScoreAltOpcodes;
1076         // This still will detect consecutive loads, but we might have "holes"
1077         // in some cases. It is ok for non-power-2 vectorization and may produce
1078         // better results. It should not affect current vectorization.
1079         return (*Dist > 0) ? VLOperands::ScoreConsecutiveLoads
1080                            : VLOperands::ScoreReversedLoads;
1081       }
1082 
1083       auto *C1 = dyn_cast<Constant>(V1);
1084       auto *C2 = dyn_cast<Constant>(V2);
1085       if (C1 && C2)
1086         return VLOperands::ScoreConstants;
1087 
1088       // Extracts from consecutive indexes of the same vector better score as
1089       // the extracts could be optimized away.
1090       Value *EV1;
1091       ConstantInt *Ex1Idx;
1092       if (match(V1, m_ExtractElt(m_Value(EV1), m_ConstantInt(Ex1Idx)))) {
1093         // Undefs are always profitable for extractelements.
1094         if (isa<UndefValue>(V2))
1095           return VLOperands::ScoreConsecutiveExtracts;
1096         Value *EV2 = nullptr;
1097         ConstantInt *Ex2Idx = nullptr;
1098         if (match(V2,
1099                   m_ExtractElt(m_Value(EV2), m_CombineOr(m_ConstantInt(Ex2Idx),
1100                                                          m_Undef())))) {
1101           // Undefs are always profitable for extractelements.
1102           if (!Ex2Idx)
1103             return VLOperands::ScoreConsecutiveExtracts;
1104           if (isUndefVector(EV2) && EV2->getType() == EV1->getType())
1105             return VLOperands::ScoreConsecutiveExtracts;
1106           if (EV2 == EV1) {
1107             int Idx1 = Ex1Idx->getZExtValue();
1108             int Idx2 = Ex2Idx->getZExtValue();
1109             int Dist = Idx2 - Idx1;
1110             // The distance is too large - still may be profitable to use
1111             // shuffles.
1112             if (std::abs(Dist) > NumLanes / 2)
1113               return VLOperands::ScoreAltOpcodes;
1114             return (Dist > 0) ? VLOperands::ScoreConsecutiveExtracts
1115                               : VLOperands::ScoreReversedExtracts;
1116           }
1117         }
1118       }
1119 
1120       auto *I1 = dyn_cast<Instruction>(V1);
1121       auto *I2 = dyn_cast<Instruction>(V2);
1122       if (I1 && I2) {
1123         if (I1->getParent() != I2->getParent())
1124           return VLOperands::ScoreFail;
1125         InstructionsState S = getSameOpcode({I1, I2});
1126         // Note: Only consider instructions with <= 2 operands to avoid
1127         // complexity explosion.
1128         if (S.getOpcode() && S.MainOp->getNumOperands() <= 2)
1129           return S.isAltShuffle() ? VLOperands::ScoreAltOpcodes
1130                                   : VLOperands::ScoreSameOpcode;
1131       }
1132 
1133       if (isa<UndefValue>(V2))
1134         return VLOperands::ScoreUndef;
1135 
1136       return VLOperands::ScoreFail;
1137     }
1138 
1139     /// Holds the values and their lanes that are taking part in the look-ahead
1140     /// score calculation. This is used in the external uses cost calculation.
1141     /// Need to hold all the lanes in case of splat/broadcast at least to
1142     /// correctly check for the use in the different lane.
1143     SmallDenseMap<Value *, SmallSet<int, 4>> InLookAheadValues;
1144 
1145     /// \returns the additional cost due to uses of \p LHS and \p RHS that are
1146     /// either external to the vectorized code, or require shuffling.
1147     int getExternalUsesCost(const std::pair<Value *, int> &LHS,
1148                             const std::pair<Value *, int> &RHS) {
1149       int Cost = 0;
1150       std::array<std::pair<Value *, int>, 2> Values = {{LHS, RHS}};
1151       for (int Idx = 0, IdxE = Values.size(); Idx != IdxE; ++Idx) {
1152         Value *V = Values[Idx].first;
1153         if (isa<Constant>(V)) {
1154           // Since this is a function pass, it doesn't make semantic sense to
1155           // walk the users of a subclass of Constant. The users could be in
1156           // another function, or even another module that happens to be in
1157           // the same LLVMContext.
1158           continue;
1159         }
1160 
1161         // Calculate the absolute lane, using the minimum relative lane of LHS
1162         // and RHS as base and Idx as the offset.
1163         int Ln = std::min(LHS.second, RHS.second) + Idx;
1164         assert(Ln >= 0 && "Bad lane calculation");
1165         unsigned UsersBudget = LookAheadUsersBudget;
1166         for (User *U : V->users()) {
1167           if (const TreeEntry *UserTE = R.getTreeEntry(U)) {
1168             // The user is in the VectorizableTree. Check if we need to insert.
1169             int UserLn = UserTE->findLaneForValue(U);
1170             assert(UserLn >= 0 && "Bad lane");
1171             // If the values are different, check just the line of the current
1172             // value. If the values are the same, need to add UserInDiffLaneCost
1173             // only if UserLn does not match both line numbers.
1174             if ((LHS.first != RHS.first && UserLn != Ln) ||
1175                 (LHS.first == RHS.first && UserLn != LHS.second &&
1176                  UserLn != RHS.second)) {
1177               Cost += UserInDiffLaneCost;
1178               break;
1179             }
1180           } else {
1181             // Check if the user is in the look-ahead code.
1182             auto It2 = InLookAheadValues.find(U);
1183             if (It2 != InLookAheadValues.end()) {
1184               // The user is in the look-ahead code. Check the lane.
1185               if (!It2->getSecond().contains(Ln)) {
1186                 Cost += UserInDiffLaneCost;
1187                 break;
1188               }
1189             } else {
1190               // The user is neither in SLP tree nor in the look-ahead code.
1191               Cost += ExternalUseCost;
1192               break;
1193             }
1194           }
1195           // Limit the number of visited uses to cap compilation time.
1196           if (--UsersBudget == 0)
1197             break;
1198         }
1199       }
1200       return Cost;
1201     }
1202 
1203     /// Go through the operands of \p LHS and \p RHS recursively until \p
1204     /// MaxLevel, and return the cummulative score. For example:
1205     /// \verbatim
1206     ///  A[0]  B[0]  A[1]  B[1]  C[0] D[0]  B[1] A[1]
1207     ///     \ /         \ /         \ /        \ /
1208     ///      +           +           +          +
1209     ///     G1          G2          G3         G4
1210     /// \endverbatim
1211     /// The getScoreAtLevelRec(G1, G2) function will try to match the nodes at
1212     /// each level recursively, accumulating the score. It starts from matching
1213     /// the additions at level 0, then moves on to the loads (level 1). The
1214     /// score of G1 and G2 is higher than G1 and G3, because {A[0],A[1]} and
1215     /// {B[0],B[1]} match with VLOperands::ScoreConsecutiveLoads, while
1216     /// {A[0],C[0]} has a score of VLOperands::ScoreFail.
1217     /// Please note that the order of the operands does not matter, as we
1218     /// evaluate the score of all profitable combinations of operands. In
1219     /// other words the score of G1 and G4 is the same as G1 and G2. This
1220     /// heuristic is based on ideas described in:
1221     ///   Look-ahead SLP: Auto-vectorization in the presence of commutative
1222     ///   operations, CGO 2018 by Vasileios Porpodas, Rodrigo C. O. Rocha,
1223     ///   Luís F. W. Góes
1224     int getScoreAtLevelRec(const std::pair<Value *, int> &LHS,
1225                            const std::pair<Value *, int> &RHS, int CurrLevel,
1226                            int MaxLevel) {
1227 
1228       Value *V1 = LHS.first;
1229       Value *V2 = RHS.first;
1230       // Get the shallow score of V1 and V2.
1231       int ShallowScoreAtThisLevel = std::max(
1232           (int)ScoreFail, getShallowScore(V1, V2, DL, SE, getNumLanes()) -
1233                               getExternalUsesCost(LHS, RHS));
1234       int Lane1 = LHS.second;
1235       int Lane2 = RHS.second;
1236 
1237       // If reached MaxLevel,
1238       //  or if V1 and V2 are not instructions,
1239       //  or if they are SPLAT,
1240       //  or if they are not consecutive,
1241       //  or if profitable to vectorize loads or extractelements, early return
1242       //  the current cost.
1243       auto *I1 = dyn_cast<Instruction>(V1);
1244       auto *I2 = dyn_cast<Instruction>(V2);
1245       if (CurrLevel == MaxLevel || !(I1 && I2) || I1 == I2 ||
1246           ShallowScoreAtThisLevel == VLOperands::ScoreFail ||
1247           (((isa<LoadInst>(I1) && isa<LoadInst>(I2)) ||
1248             (isa<ExtractElementInst>(I1) && isa<ExtractElementInst>(I2))) &&
1249            ShallowScoreAtThisLevel))
1250         return ShallowScoreAtThisLevel;
1251       assert(I1 && I2 && "Should have early exited.");
1252 
1253       // Keep track of in-tree values for determining the external-use cost.
1254       InLookAheadValues[V1].insert(Lane1);
1255       InLookAheadValues[V2].insert(Lane2);
1256 
1257       // Contains the I2 operand indexes that got matched with I1 operands.
1258       SmallSet<unsigned, 4> Op2Used;
1259 
1260       // Recursion towards the operands of I1 and I2. We are trying all possible
1261       // operand pairs, and keeping track of the best score.
1262       for (unsigned OpIdx1 = 0, NumOperands1 = I1->getNumOperands();
1263            OpIdx1 != NumOperands1; ++OpIdx1) {
1264         // Try to pair op1I with the best operand of I2.
1265         int MaxTmpScore = 0;
1266         unsigned MaxOpIdx2 = 0;
1267         bool FoundBest = false;
1268         // If I2 is commutative try all combinations.
1269         unsigned FromIdx = isCommutative(I2) ? 0 : OpIdx1;
1270         unsigned ToIdx = isCommutative(I2)
1271                              ? I2->getNumOperands()
1272                              : std::min(I2->getNumOperands(), OpIdx1 + 1);
1273         assert(FromIdx <= ToIdx && "Bad index");
1274         for (unsigned OpIdx2 = FromIdx; OpIdx2 != ToIdx; ++OpIdx2) {
1275           // Skip operands already paired with OpIdx1.
1276           if (Op2Used.count(OpIdx2))
1277             continue;
1278           // Recursively calculate the cost at each level
1279           int TmpScore = getScoreAtLevelRec({I1->getOperand(OpIdx1), Lane1},
1280                                             {I2->getOperand(OpIdx2), Lane2},
1281                                             CurrLevel + 1, MaxLevel);
1282           // Look for the best score.
1283           if (TmpScore > VLOperands::ScoreFail && TmpScore > MaxTmpScore) {
1284             MaxTmpScore = TmpScore;
1285             MaxOpIdx2 = OpIdx2;
1286             FoundBest = true;
1287           }
1288         }
1289         if (FoundBest) {
1290           // Pair {OpIdx1, MaxOpIdx2} was found to be best. Never revisit it.
1291           Op2Used.insert(MaxOpIdx2);
1292           ShallowScoreAtThisLevel += MaxTmpScore;
1293         }
1294       }
1295       return ShallowScoreAtThisLevel;
1296     }
1297 
1298     /// \Returns the look-ahead score, which tells us how much the sub-trees
1299     /// rooted at \p LHS and \p RHS match, the more they match the higher the
1300     /// score. This helps break ties in an informed way when we cannot decide on
1301     /// the order of the operands by just considering the immediate
1302     /// predecessors.
1303     int getLookAheadScore(const std::pair<Value *, int> &LHS,
1304                           const std::pair<Value *, int> &RHS) {
1305       InLookAheadValues.clear();
1306       return getScoreAtLevelRec(LHS, RHS, 1, LookAheadMaxDepth);
1307     }
1308 
1309     // Search all operands in Ops[*][Lane] for the one that matches best
1310     // Ops[OpIdx][LastLane] and return its opreand index.
1311     // If no good match can be found, return None.
1312     Optional<unsigned>
1313     getBestOperand(unsigned OpIdx, int Lane, int LastLane,
1314                    ArrayRef<ReorderingMode> ReorderingModes) {
1315       unsigned NumOperands = getNumOperands();
1316 
1317       // The operand of the previous lane at OpIdx.
1318       Value *OpLastLane = getData(OpIdx, LastLane).V;
1319 
1320       // Our strategy mode for OpIdx.
1321       ReorderingMode RMode = ReorderingModes[OpIdx];
1322 
1323       // The linearized opcode of the operand at OpIdx, Lane.
1324       bool OpIdxAPO = getData(OpIdx, Lane).APO;
1325 
1326       // The best operand index and its score.
1327       // Sometimes we have more than one option (e.g., Opcode and Undefs), so we
1328       // are using the score to differentiate between the two.
1329       struct BestOpData {
1330         Optional<unsigned> Idx = None;
1331         unsigned Score = 0;
1332       } BestOp;
1333 
1334       // Iterate through all unused operands and look for the best.
1335       for (unsigned Idx = 0; Idx != NumOperands; ++Idx) {
1336         // Get the operand at Idx and Lane.
1337         OperandData &OpData = getData(Idx, Lane);
1338         Value *Op = OpData.V;
1339         bool OpAPO = OpData.APO;
1340 
1341         // Skip already selected operands.
1342         if (OpData.IsUsed)
1343           continue;
1344 
1345         // Skip if we are trying to move the operand to a position with a
1346         // different opcode in the linearized tree form. This would break the
1347         // semantics.
1348         if (OpAPO != OpIdxAPO)
1349           continue;
1350 
1351         // Look for an operand that matches the current mode.
1352         switch (RMode) {
1353         case ReorderingMode::Load:
1354         case ReorderingMode::Constant:
1355         case ReorderingMode::Opcode: {
1356           bool LeftToRight = Lane > LastLane;
1357           Value *OpLeft = (LeftToRight) ? OpLastLane : Op;
1358           Value *OpRight = (LeftToRight) ? Op : OpLastLane;
1359           unsigned Score =
1360               getLookAheadScore({OpLeft, LastLane}, {OpRight, Lane});
1361           if (Score > BestOp.Score) {
1362             BestOp.Idx = Idx;
1363             BestOp.Score = Score;
1364           }
1365           break;
1366         }
1367         case ReorderingMode::Splat:
1368           if (Op == OpLastLane)
1369             BestOp.Idx = Idx;
1370           break;
1371         case ReorderingMode::Failed:
1372           return None;
1373         }
1374       }
1375 
1376       if (BestOp.Idx) {
1377         getData(BestOp.Idx.getValue(), Lane).IsUsed = true;
1378         return BestOp.Idx;
1379       }
1380       // If we could not find a good match return None.
1381       return None;
1382     }
1383 
1384     /// Helper for reorderOperandVecs.
1385     /// \returns the lane that we should start reordering from. This is the one
1386     /// which has the least number of operands that can freely move about or
1387     /// less profitable because it already has the most optimal set of operands.
1388     unsigned getBestLaneToStartReordering() const {
1389       unsigned Min = UINT_MAX;
1390       unsigned SameOpNumber = 0;
1391       // std::pair<unsigned, unsigned> is used to implement a simple voting
1392       // algorithm and choose the lane with the least number of operands that
1393       // can freely move about or less profitable because it already has the
1394       // most optimal set of operands. The first unsigned is a counter for
1395       // voting, the second unsigned is the counter of lanes with instructions
1396       // with same/alternate opcodes and same parent basic block.
1397       MapVector<unsigned, std::pair<unsigned, unsigned>> HashMap;
1398       // Try to be closer to the original results, if we have multiple lanes
1399       // with same cost. If 2 lanes have the same cost, use the one with the
1400       // lowest index.
1401       for (int I = getNumLanes(); I > 0; --I) {
1402         unsigned Lane = I - 1;
1403         OperandsOrderData NumFreeOpsHash =
1404             getMaxNumOperandsThatCanBeReordered(Lane);
1405         // Compare the number of operands that can move and choose the one with
1406         // the least number.
1407         if (NumFreeOpsHash.NumOfAPOs < Min) {
1408           Min = NumFreeOpsHash.NumOfAPOs;
1409           SameOpNumber = NumFreeOpsHash.NumOpsWithSameOpcodeParent;
1410           HashMap.clear();
1411           HashMap[NumFreeOpsHash.Hash] = std::make_pair(1, Lane);
1412         } else if (NumFreeOpsHash.NumOfAPOs == Min &&
1413                    NumFreeOpsHash.NumOpsWithSameOpcodeParent < SameOpNumber) {
1414           // Select the most optimal lane in terms of number of operands that
1415           // should be moved around.
1416           SameOpNumber = NumFreeOpsHash.NumOpsWithSameOpcodeParent;
1417           HashMap[NumFreeOpsHash.Hash] = std::make_pair(1, Lane);
1418         } else if (NumFreeOpsHash.NumOfAPOs == Min &&
1419                    NumFreeOpsHash.NumOpsWithSameOpcodeParent == SameOpNumber) {
1420           auto It = HashMap.find(NumFreeOpsHash.Hash);
1421           if (It == HashMap.end())
1422             HashMap[NumFreeOpsHash.Hash] = std::make_pair(1, Lane);
1423           else
1424             ++It->second.first;
1425         }
1426       }
1427       // Select the lane with the minimum counter.
1428       unsigned BestLane = 0;
1429       unsigned CntMin = UINT_MAX;
1430       for (const auto &Data : reverse(HashMap)) {
1431         if (Data.second.first < CntMin) {
1432           CntMin = Data.second.first;
1433           BestLane = Data.second.second;
1434         }
1435       }
1436       return BestLane;
1437     }
1438 
1439     /// Data structure that helps to reorder operands.
1440     struct OperandsOrderData {
1441       /// The best number of operands with the same APOs, which can be
1442       /// reordered.
1443       unsigned NumOfAPOs = UINT_MAX;
1444       /// Number of operands with the same/alternate instruction opcode and
1445       /// parent.
1446       unsigned NumOpsWithSameOpcodeParent = 0;
1447       /// Hash for the actual operands ordering.
1448       /// Used to count operands, actually their position id and opcode
1449       /// value. It is used in the voting mechanism to find the lane with the
1450       /// least number of operands that can freely move about or less profitable
1451       /// because it already has the most optimal set of operands. Can be
1452       /// replaced with SmallVector<unsigned> instead but hash code is faster
1453       /// and requires less memory.
1454       unsigned Hash = 0;
1455     };
1456     /// \returns the maximum number of operands that are allowed to be reordered
1457     /// for \p Lane and the number of compatible instructions(with the same
1458     /// parent/opcode). This is used as a heuristic for selecting the first lane
1459     /// to start operand reordering.
1460     OperandsOrderData getMaxNumOperandsThatCanBeReordered(unsigned Lane) const {
1461       unsigned CntTrue = 0;
1462       unsigned NumOperands = getNumOperands();
1463       // Operands with the same APO can be reordered. We therefore need to count
1464       // how many of them we have for each APO, like this: Cnt[APO] = x.
1465       // Since we only have two APOs, namely true and false, we can avoid using
1466       // a map. Instead we can simply count the number of operands that
1467       // correspond to one of them (in this case the 'true' APO), and calculate
1468       // the other by subtracting it from the total number of operands.
1469       // Operands with the same instruction opcode and parent are more
1470       // profitable since we don't need to move them in many cases, with a high
1471       // probability such lane already can be vectorized effectively.
1472       bool AllUndefs = true;
1473       unsigned NumOpsWithSameOpcodeParent = 0;
1474       Instruction *OpcodeI = nullptr;
1475       BasicBlock *Parent = nullptr;
1476       unsigned Hash = 0;
1477       for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
1478         const OperandData &OpData = getData(OpIdx, Lane);
1479         if (OpData.APO)
1480           ++CntTrue;
1481         // Use Boyer-Moore majority voting for finding the majority opcode and
1482         // the number of times it occurs.
1483         if (auto *I = dyn_cast<Instruction>(OpData.V)) {
1484           if (!OpcodeI || !getSameOpcode({OpcodeI, I}).getOpcode() ||
1485               I->getParent() != Parent) {
1486             if (NumOpsWithSameOpcodeParent == 0) {
1487               NumOpsWithSameOpcodeParent = 1;
1488               OpcodeI = I;
1489               Parent = I->getParent();
1490             } else {
1491               --NumOpsWithSameOpcodeParent;
1492             }
1493           } else {
1494             ++NumOpsWithSameOpcodeParent;
1495           }
1496         }
1497         Hash = hash_combine(
1498             Hash, hash_value((OpIdx + 1) * (OpData.V->getValueID() + 1)));
1499         AllUndefs = AllUndefs && isa<UndefValue>(OpData.V);
1500       }
1501       if (AllUndefs)
1502         return {};
1503       OperandsOrderData Data;
1504       Data.NumOfAPOs = std::max(CntTrue, NumOperands - CntTrue);
1505       Data.NumOpsWithSameOpcodeParent = NumOpsWithSameOpcodeParent;
1506       Data.Hash = Hash;
1507       return Data;
1508     }
1509 
1510     /// Go through the instructions in VL and append their operands.
1511     void appendOperandsOfVL(ArrayRef<Value *> VL) {
1512       assert(!VL.empty() && "Bad VL");
1513       assert((empty() || VL.size() == getNumLanes()) &&
1514              "Expected same number of lanes");
1515       assert(isa<Instruction>(VL[0]) && "Expected instruction");
1516       unsigned NumOperands = cast<Instruction>(VL[0])->getNumOperands();
1517       OpsVec.resize(NumOperands);
1518       unsigned NumLanes = VL.size();
1519       for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
1520         OpsVec[OpIdx].resize(NumLanes);
1521         for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1522           assert(isa<Instruction>(VL[Lane]) && "Expected instruction");
1523           // Our tree has just 3 nodes: the root and two operands.
1524           // It is therefore trivial to get the APO. We only need to check the
1525           // opcode of VL[Lane] and whether the operand at OpIdx is the LHS or
1526           // RHS operand. The LHS operand of both add and sub is never attached
1527           // to an inversese operation in the linearized form, therefore its APO
1528           // is false. The RHS is true only if VL[Lane] is an inverse operation.
1529 
1530           // Since operand reordering is performed on groups of commutative
1531           // operations or alternating sequences (e.g., +, -), we can safely
1532           // tell the inverse operations by checking commutativity.
1533           bool IsInverseOperation = !isCommutative(cast<Instruction>(VL[Lane]));
1534           bool APO = (OpIdx == 0) ? false : IsInverseOperation;
1535           OpsVec[OpIdx][Lane] = {cast<Instruction>(VL[Lane])->getOperand(OpIdx),
1536                                  APO, false};
1537         }
1538       }
1539     }
1540 
1541     /// \returns the number of operands.
1542     unsigned getNumOperands() const { return OpsVec.size(); }
1543 
1544     /// \returns the number of lanes.
1545     unsigned getNumLanes() const { return OpsVec[0].size(); }
1546 
1547     /// \returns the operand value at \p OpIdx and \p Lane.
1548     Value *getValue(unsigned OpIdx, unsigned Lane) const {
1549       return getData(OpIdx, Lane).V;
1550     }
1551 
1552     /// \returns true if the data structure is empty.
1553     bool empty() const { return OpsVec.empty(); }
1554 
1555     /// Clears the data.
1556     void clear() { OpsVec.clear(); }
1557 
1558     /// \Returns true if there are enough operands identical to \p Op to fill
1559     /// the whole vector.
1560     /// Note: This modifies the 'IsUsed' flag, so a cleanUsed() must follow.
1561     bool shouldBroadcast(Value *Op, unsigned OpIdx, unsigned Lane) {
1562       bool OpAPO = getData(OpIdx, Lane).APO;
1563       for (unsigned Ln = 0, Lns = getNumLanes(); Ln != Lns; ++Ln) {
1564         if (Ln == Lane)
1565           continue;
1566         // This is set to true if we found a candidate for broadcast at Lane.
1567         bool FoundCandidate = false;
1568         for (unsigned OpI = 0, OpE = getNumOperands(); OpI != OpE; ++OpI) {
1569           OperandData &Data = getData(OpI, Ln);
1570           if (Data.APO != OpAPO || Data.IsUsed)
1571             continue;
1572           if (Data.V == Op) {
1573             FoundCandidate = true;
1574             Data.IsUsed = true;
1575             break;
1576           }
1577         }
1578         if (!FoundCandidate)
1579           return false;
1580       }
1581       return true;
1582     }
1583 
1584   public:
1585     /// Initialize with all the operands of the instruction vector \p RootVL.
1586     VLOperands(ArrayRef<Value *> RootVL, const DataLayout &DL,
1587                ScalarEvolution &SE, const BoUpSLP &R)
1588         : DL(DL), SE(SE), R(R) {
1589       // Append all the operands of RootVL.
1590       appendOperandsOfVL(RootVL);
1591     }
1592 
1593     /// \Returns a value vector with the operands across all lanes for the
1594     /// opearnd at \p OpIdx.
1595     ValueList getVL(unsigned OpIdx) const {
1596       ValueList OpVL(OpsVec[OpIdx].size());
1597       assert(OpsVec[OpIdx].size() == getNumLanes() &&
1598              "Expected same num of lanes across all operands");
1599       for (unsigned Lane = 0, Lanes = getNumLanes(); Lane != Lanes; ++Lane)
1600         OpVL[Lane] = OpsVec[OpIdx][Lane].V;
1601       return OpVL;
1602     }
1603 
1604     // Performs operand reordering for 2 or more operands.
1605     // The original operands are in OrigOps[OpIdx][Lane].
1606     // The reordered operands are returned in 'SortedOps[OpIdx][Lane]'.
1607     void reorder() {
1608       unsigned NumOperands = getNumOperands();
1609       unsigned NumLanes = getNumLanes();
1610       // Each operand has its own mode. We are using this mode to help us select
1611       // the instructions for each lane, so that they match best with the ones
1612       // we have selected so far.
1613       SmallVector<ReorderingMode, 2> ReorderingModes(NumOperands);
1614 
1615       // This is a greedy single-pass algorithm. We are going over each lane
1616       // once and deciding on the best order right away with no back-tracking.
1617       // However, in order to increase its effectiveness, we start with the lane
1618       // that has operands that can move the least. For example, given the
1619       // following lanes:
1620       //  Lane 0 : A[0] = B[0] + C[0]   // Visited 3rd
1621       //  Lane 1 : A[1] = C[1] - B[1]   // Visited 1st
1622       //  Lane 2 : A[2] = B[2] + C[2]   // Visited 2nd
1623       //  Lane 3 : A[3] = C[3] - B[3]   // Visited 4th
1624       // we will start at Lane 1, since the operands of the subtraction cannot
1625       // be reordered. Then we will visit the rest of the lanes in a circular
1626       // fashion. That is, Lanes 2, then Lane 0, and finally Lane 3.
1627 
1628       // Find the first lane that we will start our search from.
1629       unsigned FirstLane = getBestLaneToStartReordering();
1630 
1631       // Initialize the modes.
1632       for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
1633         Value *OpLane0 = getValue(OpIdx, FirstLane);
1634         // Keep track if we have instructions with all the same opcode on one
1635         // side.
1636         if (isa<LoadInst>(OpLane0))
1637           ReorderingModes[OpIdx] = ReorderingMode::Load;
1638         else if (isa<Instruction>(OpLane0)) {
1639           // Check if OpLane0 should be broadcast.
1640           if (shouldBroadcast(OpLane0, OpIdx, FirstLane))
1641             ReorderingModes[OpIdx] = ReorderingMode::Splat;
1642           else
1643             ReorderingModes[OpIdx] = ReorderingMode::Opcode;
1644         }
1645         else if (isa<Constant>(OpLane0))
1646           ReorderingModes[OpIdx] = ReorderingMode::Constant;
1647         else if (isa<Argument>(OpLane0))
1648           // Our best hope is a Splat. It may save some cost in some cases.
1649           ReorderingModes[OpIdx] = ReorderingMode::Splat;
1650         else
1651           // NOTE: This should be unreachable.
1652           ReorderingModes[OpIdx] = ReorderingMode::Failed;
1653       }
1654 
1655       // Check that we don't have same operands. No need to reorder if operands
1656       // are just perfect diamond or shuffled diamond match. Do not do it only
1657       // for possible broadcasts or non-power of 2 number of scalars (just for
1658       // now).
1659       auto &&SkipReordering = [this]() {
1660         SmallPtrSet<Value *, 4> UniqueValues;
1661         ArrayRef<OperandData> Op0 = OpsVec.front();
1662         for (const OperandData &Data : Op0)
1663           UniqueValues.insert(Data.V);
1664         for (ArrayRef<OperandData> Op : drop_begin(OpsVec, 1)) {
1665           if (any_of(Op, [&UniqueValues](const OperandData &Data) {
1666                 return !UniqueValues.contains(Data.V);
1667               }))
1668             return false;
1669         }
1670         // TODO: Check if we can remove a check for non-power-2 number of
1671         // scalars after full support of non-power-2 vectorization.
1672         return UniqueValues.size() != 2 && isPowerOf2_32(UniqueValues.size());
1673       };
1674 
1675       // If the initial strategy fails for any of the operand indexes, then we
1676       // perform reordering again in a second pass. This helps avoid assigning
1677       // high priority to the failed strategy, and should improve reordering for
1678       // the non-failed operand indexes.
1679       for (int Pass = 0; Pass != 2; ++Pass) {
1680         // Check if no need to reorder operands since they're are perfect or
1681         // shuffled diamond match.
1682         // Need to to do it to avoid extra external use cost counting for
1683         // shuffled matches, which may cause regressions.
1684         if (SkipReordering())
1685           break;
1686         // Skip the second pass if the first pass did not fail.
1687         bool StrategyFailed = false;
1688         // Mark all operand data as free to use.
1689         clearUsed();
1690         // We keep the original operand order for the FirstLane, so reorder the
1691         // rest of the lanes. We are visiting the nodes in a circular fashion,
1692         // using FirstLane as the center point and increasing the radius
1693         // distance.
1694         for (unsigned Distance = 1; Distance != NumLanes; ++Distance) {
1695           // Visit the lane on the right and then the lane on the left.
1696           for (int Direction : {+1, -1}) {
1697             int Lane = FirstLane + Direction * Distance;
1698             if (Lane < 0 || Lane >= (int)NumLanes)
1699               continue;
1700             int LastLane = Lane - Direction;
1701             assert(LastLane >= 0 && LastLane < (int)NumLanes &&
1702                    "Out of bounds");
1703             // Look for a good match for each operand.
1704             for (unsigned OpIdx = 0; OpIdx != NumOperands; ++OpIdx) {
1705               // Search for the operand that matches SortedOps[OpIdx][Lane-1].
1706               Optional<unsigned> BestIdx =
1707                   getBestOperand(OpIdx, Lane, LastLane, ReorderingModes);
1708               // By not selecting a value, we allow the operands that follow to
1709               // select a better matching value. We will get a non-null value in
1710               // the next run of getBestOperand().
1711               if (BestIdx) {
1712                 // Swap the current operand with the one returned by
1713                 // getBestOperand().
1714                 swap(OpIdx, BestIdx.getValue(), Lane);
1715               } else {
1716                 // We failed to find a best operand, set mode to 'Failed'.
1717                 ReorderingModes[OpIdx] = ReorderingMode::Failed;
1718                 // Enable the second pass.
1719                 StrategyFailed = true;
1720               }
1721             }
1722           }
1723         }
1724         // Skip second pass if the strategy did not fail.
1725         if (!StrategyFailed)
1726           break;
1727       }
1728     }
1729 
1730 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1731     LLVM_DUMP_METHOD static StringRef getModeStr(ReorderingMode RMode) {
1732       switch (RMode) {
1733       case ReorderingMode::Load:
1734         return "Load";
1735       case ReorderingMode::Opcode:
1736         return "Opcode";
1737       case ReorderingMode::Constant:
1738         return "Constant";
1739       case ReorderingMode::Splat:
1740         return "Splat";
1741       case ReorderingMode::Failed:
1742         return "Failed";
1743       }
1744       llvm_unreachable("Unimplemented Reordering Type");
1745     }
1746 
1747     LLVM_DUMP_METHOD static raw_ostream &printMode(ReorderingMode RMode,
1748                                                    raw_ostream &OS) {
1749       return OS << getModeStr(RMode);
1750     }
1751 
1752     /// Debug print.
1753     LLVM_DUMP_METHOD static void dumpMode(ReorderingMode RMode) {
1754       printMode(RMode, dbgs());
1755     }
1756 
1757     friend raw_ostream &operator<<(raw_ostream &OS, ReorderingMode RMode) {
1758       return printMode(RMode, OS);
1759     }
1760 
1761     LLVM_DUMP_METHOD raw_ostream &print(raw_ostream &OS) const {
1762       const unsigned Indent = 2;
1763       unsigned Cnt = 0;
1764       for (const OperandDataVec &OpDataVec : OpsVec) {
1765         OS << "Operand " << Cnt++ << "\n";
1766         for (const OperandData &OpData : OpDataVec) {
1767           OS.indent(Indent) << "{";
1768           if (Value *V = OpData.V)
1769             OS << *V;
1770           else
1771             OS << "null";
1772           OS << ", APO:" << OpData.APO << "}\n";
1773         }
1774         OS << "\n";
1775       }
1776       return OS;
1777     }
1778 
1779     /// Debug print.
1780     LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
1781 #endif
1782   };
1783 
1784   /// Checks if the instruction is marked for deletion.
1785   bool isDeleted(Instruction *I) const { return DeletedInstructions.count(I); }
1786 
1787   /// Marks values operands for later deletion by replacing them with Undefs.
1788   void eraseInstructions(ArrayRef<Value *> AV);
1789 
1790   ~BoUpSLP();
1791 
1792 private:
1793   /// Checks if all users of \p I are the part of the vectorization tree.
1794   bool areAllUsersVectorized(Instruction *I,
1795                              ArrayRef<Value *> VectorizedVals) const;
1796 
1797   /// \returns the cost of the vectorizable entry.
1798   InstructionCost getEntryCost(const TreeEntry *E,
1799                                ArrayRef<Value *> VectorizedVals);
1800 
1801   /// This is the recursive part of buildTree.
1802   void buildTree_rec(ArrayRef<Value *> Roots, unsigned Depth,
1803                      const EdgeInfo &EI);
1804 
1805   /// \returns true if the ExtractElement/ExtractValue instructions in \p VL can
1806   /// be vectorized to use the original vector (or aggregate "bitcast" to a
1807   /// vector) and sets \p CurrentOrder to the identity permutation; otherwise
1808   /// returns false, setting \p CurrentOrder to either an empty vector or a
1809   /// non-identity permutation that allows to reuse extract instructions.
1810   bool canReuseExtract(ArrayRef<Value *> VL, Value *OpValue,
1811                        SmallVectorImpl<unsigned> &CurrentOrder) const;
1812 
1813   /// Vectorize a single entry in the tree.
1814   Value *vectorizeTree(TreeEntry *E);
1815 
1816   /// Vectorize a single entry in the tree, starting in \p VL.
1817   Value *vectorizeTree(ArrayRef<Value *> VL);
1818 
1819   /// \returns the scalarization cost for this type. Scalarization in this
1820   /// context means the creation of vectors from a group of scalars. If \p
1821   /// NeedToShuffle is true, need to add a cost of reshuffling some of the
1822   /// vector elements.
1823   InstructionCost getGatherCost(FixedVectorType *Ty,
1824                                 const DenseSet<unsigned> &ShuffledIndices,
1825                                 bool NeedToShuffle) const;
1826 
1827   /// Checks if the gathered \p VL can be represented as shuffle(s) of previous
1828   /// tree entries.
1829   /// \returns ShuffleKind, if gathered values can be represented as shuffles of
1830   /// previous tree entries. \p Mask is filled with the shuffle mask.
1831   Optional<TargetTransformInfo::ShuffleKind>
1832   isGatherShuffledEntry(const TreeEntry *TE, SmallVectorImpl<int> &Mask,
1833                         SmallVectorImpl<const TreeEntry *> &Entries);
1834 
1835   /// \returns the scalarization cost for this list of values. Assuming that
1836   /// this subtree gets vectorized, we may need to extract the values from the
1837   /// roots. This method calculates the cost of extracting the values.
1838   InstructionCost getGatherCost(ArrayRef<Value *> VL) const;
1839 
1840   /// Set the Builder insert point to one after the last instruction in
1841   /// the bundle
1842   void setInsertPointAfterBundle(const TreeEntry *E);
1843 
1844   /// \returns a vector from a collection of scalars in \p VL.
1845   Value *gather(ArrayRef<Value *> VL);
1846 
1847   /// \returns whether the VectorizableTree is fully vectorizable and will
1848   /// be beneficial even the tree height is tiny.
1849   bool isFullyVectorizableTinyTree(bool ForReduction) const;
1850 
1851   /// Reorder commutative or alt operands to get better probability of
1852   /// generating vectorized code.
1853   static void reorderInputsAccordingToOpcode(ArrayRef<Value *> VL,
1854                                              SmallVectorImpl<Value *> &Left,
1855                                              SmallVectorImpl<Value *> &Right,
1856                                              const DataLayout &DL,
1857                                              ScalarEvolution &SE,
1858                                              const BoUpSLP &R);
1859   struct TreeEntry {
1860     using VecTreeTy = SmallVector<std::unique_ptr<TreeEntry>, 8>;
1861     TreeEntry(VecTreeTy &Container) : Container(Container) {}
1862 
1863     /// \returns true if the scalars in VL are equal to this entry.
1864     bool isSame(ArrayRef<Value *> VL) const {
1865       auto &&IsSame = [VL](ArrayRef<Value *> Scalars, ArrayRef<int> Mask) {
1866         if (Mask.size() != VL.size() && VL.size() == Scalars.size())
1867           return std::equal(VL.begin(), VL.end(), Scalars.begin());
1868         return VL.size() == Mask.size() &&
1869                std::equal(VL.begin(), VL.end(), Mask.begin(),
1870                           [Scalars](Value *V, int Idx) {
1871                             return (isa<UndefValue>(V) &&
1872                                     Idx == UndefMaskElem) ||
1873                                    (Idx != UndefMaskElem && V == Scalars[Idx]);
1874                           });
1875       };
1876       if (!ReorderIndices.empty()) {
1877         // TODO: implement matching if the nodes are just reordered, still can
1878         // treat the vector as the same if the list of scalars matches VL
1879         // directly, without reordering.
1880         SmallVector<int> Mask;
1881         inversePermutation(ReorderIndices, Mask);
1882         if (VL.size() == Scalars.size())
1883           return IsSame(Scalars, Mask);
1884         if (VL.size() == ReuseShuffleIndices.size()) {
1885           ::addMask(Mask, ReuseShuffleIndices);
1886           return IsSame(Scalars, Mask);
1887         }
1888         return false;
1889       }
1890       return IsSame(Scalars, ReuseShuffleIndices);
1891     }
1892 
1893     /// \returns true if current entry has same operands as \p TE.
1894     bool hasEqualOperands(const TreeEntry &TE) const {
1895       if (TE.getNumOperands() != getNumOperands())
1896         return false;
1897       SmallBitVector Used(getNumOperands());
1898       for (unsigned I = 0, E = getNumOperands(); I < E; ++I) {
1899         unsigned PrevCount = Used.count();
1900         for (unsigned K = 0; K < E; ++K) {
1901           if (Used.test(K))
1902             continue;
1903           if (getOperand(K) == TE.getOperand(I)) {
1904             Used.set(K);
1905             break;
1906           }
1907         }
1908         // Check if we actually found the matching operand.
1909         if (PrevCount == Used.count())
1910           return false;
1911       }
1912       return true;
1913     }
1914 
1915     /// \return Final vectorization factor for the node. Defined by the total
1916     /// number of vectorized scalars, including those, used several times in the
1917     /// entry and counted in the \a ReuseShuffleIndices, if any.
1918     unsigned getVectorFactor() const {
1919       if (!ReuseShuffleIndices.empty())
1920         return ReuseShuffleIndices.size();
1921       return Scalars.size();
1922     };
1923 
1924     /// A vector of scalars.
1925     ValueList Scalars;
1926 
1927     /// The Scalars are vectorized into this value. It is initialized to Null.
1928     Value *VectorizedValue = nullptr;
1929 
1930     /// Do we need to gather this sequence or vectorize it
1931     /// (either with vector instruction or with scatter/gather
1932     /// intrinsics for store/load)?
1933     enum EntryState { Vectorize, ScatterVectorize, NeedToGather };
1934     EntryState State;
1935 
1936     /// Does this sequence require some shuffling?
1937     SmallVector<int, 4> ReuseShuffleIndices;
1938 
1939     /// Does this entry require reordering?
1940     SmallVector<unsigned, 4> ReorderIndices;
1941 
1942     /// Points back to the VectorizableTree.
1943     ///
1944     /// Only used for Graphviz right now.  Unfortunately GraphTrait::NodeRef has
1945     /// to be a pointer and needs to be able to initialize the child iterator.
1946     /// Thus we need a reference back to the container to translate the indices
1947     /// to entries.
1948     VecTreeTy &Container;
1949 
1950     /// The TreeEntry index containing the user of this entry.  We can actually
1951     /// have multiple users so the data structure is not truly a tree.
1952     SmallVector<EdgeInfo, 1> UserTreeIndices;
1953 
1954     /// The index of this treeEntry in VectorizableTree.
1955     int Idx = -1;
1956 
1957   private:
1958     /// The operands of each instruction in each lane Operands[op_index][lane].
1959     /// Note: This helps avoid the replication of the code that performs the
1960     /// reordering of operands during buildTree_rec() and vectorizeTree().
1961     SmallVector<ValueList, 2> Operands;
1962 
1963     /// The main/alternate instruction.
1964     Instruction *MainOp = nullptr;
1965     Instruction *AltOp = nullptr;
1966 
1967   public:
1968     /// Set this bundle's \p OpIdx'th operand to \p OpVL.
1969     void setOperand(unsigned OpIdx, ArrayRef<Value *> OpVL) {
1970       if (Operands.size() < OpIdx + 1)
1971         Operands.resize(OpIdx + 1);
1972       assert(Operands[OpIdx].empty() && "Already resized?");
1973       assert(OpVL.size() <= Scalars.size() &&
1974              "Number of operands is greater than the number of scalars.");
1975       Operands[OpIdx].resize(OpVL.size());
1976       copy(OpVL, Operands[OpIdx].begin());
1977     }
1978 
1979     /// Set the operands of this bundle in their original order.
1980     void setOperandsInOrder() {
1981       assert(Operands.empty() && "Already initialized?");
1982       auto *I0 = cast<Instruction>(Scalars[0]);
1983       Operands.resize(I0->getNumOperands());
1984       unsigned NumLanes = Scalars.size();
1985       for (unsigned OpIdx = 0, NumOperands = I0->getNumOperands();
1986            OpIdx != NumOperands; ++OpIdx) {
1987         Operands[OpIdx].resize(NumLanes);
1988         for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
1989           auto *I = cast<Instruction>(Scalars[Lane]);
1990           assert(I->getNumOperands() == NumOperands &&
1991                  "Expected same number of operands");
1992           Operands[OpIdx][Lane] = I->getOperand(OpIdx);
1993         }
1994       }
1995     }
1996 
1997     /// Reorders operands of the node to the given mask \p Mask.
1998     void reorderOperands(ArrayRef<int> Mask) {
1999       for (ValueList &Operand : Operands)
2000         reorderScalars(Operand, Mask);
2001     }
2002 
2003     /// \returns the \p OpIdx operand of this TreeEntry.
2004     ValueList &getOperand(unsigned OpIdx) {
2005       assert(OpIdx < Operands.size() && "Off bounds");
2006       return Operands[OpIdx];
2007     }
2008 
2009     /// \returns the \p OpIdx operand of this TreeEntry.
2010     ArrayRef<Value *> getOperand(unsigned OpIdx) const {
2011       assert(OpIdx < Operands.size() && "Off bounds");
2012       return Operands[OpIdx];
2013     }
2014 
2015     /// \returns the number of operands.
2016     unsigned getNumOperands() const { return Operands.size(); }
2017 
2018     /// \return the single \p OpIdx operand.
2019     Value *getSingleOperand(unsigned OpIdx) const {
2020       assert(OpIdx < Operands.size() && "Off bounds");
2021       assert(!Operands[OpIdx].empty() && "No operand available");
2022       return Operands[OpIdx][0];
2023     }
2024 
2025     /// Some of the instructions in the list have alternate opcodes.
2026     bool isAltShuffle() const { return MainOp != AltOp; }
2027 
2028     bool isOpcodeOrAlt(Instruction *I) const {
2029       unsigned CheckedOpcode = I->getOpcode();
2030       return (getOpcode() == CheckedOpcode ||
2031               getAltOpcode() == CheckedOpcode);
2032     }
2033 
2034     /// Chooses the correct key for scheduling data. If \p Op has the same (or
2035     /// alternate) opcode as \p OpValue, the key is \p Op. Otherwise the key is
2036     /// \p OpValue.
2037     Value *isOneOf(Value *Op) const {
2038       auto *I = dyn_cast<Instruction>(Op);
2039       if (I && isOpcodeOrAlt(I))
2040         return Op;
2041       return MainOp;
2042     }
2043 
2044     void setOperations(const InstructionsState &S) {
2045       MainOp = S.MainOp;
2046       AltOp = S.AltOp;
2047     }
2048 
2049     Instruction *getMainOp() const {
2050       return MainOp;
2051     }
2052 
2053     Instruction *getAltOp() const {
2054       return AltOp;
2055     }
2056 
2057     /// The main/alternate opcodes for the list of instructions.
2058     unsigned getOpcode() const {
2059       return MainOp ? MainOp->getOpcode() : 0;
2060     }
2061 
2062     unsigned getAltOpcode() const {
2063       return AltOp ? AltOp->getOpcode() : 0;
2064     }
2065 
2066     /// When ReuseReorderShuffleIndices is empty it just returns position of \p
2067     /// V within vector of Scalars. Otherwise, try to remap on its reuse index.
2068     int findLaneForValue(Value *V) const {
2069       unsigned FoundLane = std::distance(Scalars.begin(), find(Scalars, V));
2070       assert(FoundLane < Scalars.size() && "Couldn't find extract lane");
2071       if (!ReorderIndices.empty())
2072         FoundLane = ReorderIndices[FoundLane];
2073       assert(FoundLane < Scalars.size() && "Couldn't find extract lane");
2074       if (!ReuseShuffleIndices.empty()) {
2075         FoundLane = std::distance(ReuseShuffleIndices.begin(),
2076                                   find(ReuseShuffleIndices, FoundLane));
2077       }
2078       return FoundLane;
2079     }
2080 
2081 #ifndef NDEBUG
2082     /// Debug printer.
2083     LLVM_DUMP_METHOD void dump() const {
2084       dbgs() << Idx << ".\n";
2085       for (unsigned OpI = 0, OpE = Operands.size(); OpI != OpE; ++OpI) {
2086         dbgs() << "Operand " << OpI << ":\n";
2087         for (const Value *V : Operands[OpI])
2088           dbgs().indent(2) << *V << "\n";
2089       }
2090       dbgs() << "Scalars: \n";
2091       for (Value *V : Scalars)
2092         dbgs().indent(2) << *V << "\n";
2093       dbgs() << "State: ";
2094       switch (State) {
2095       case Vectorize:
2096         dbgs() << "Vectorize\n";
2097         break;
2098       case ScatterVectorize:
2099         dbgs() << "ScatterVectorize\n";
2100         break;
2101       case NeedToGather:
2102         dbgs() << "NeedToGather\n";
2103         break;
2104       }
2105       dbgs() << "MainOp: ";
2106       if (MainOp)
2107         dbgs() << *MainOp << "\n";
2108       else
2109         dbgs() << "NULL\n";
2110       dbgs() << "AltOp: ";
2111       if (AltOp)
2112         dbgs() << *AltOp << "\n";
2113       else
2114         dbgs() << "NULL\n";
2115       dbgs() << "VectorizedValue: ";
2116       if (VectorizedValue)
2117         dbgs() << *VectorizedValue << "\n";
2118       else
2119         dbgs() << "NULL\n";
2120       dbgs() << "ReuseShuffleIndices: ";
2121       if (ReuseShuffleIndices.empty())
2122         dbgs() << "Empty";
2123       else
2124         for (int ReuseIdx : ReuseShuffleIndices)
2125           dbgs() << ReuseIdx << ", ";
2126       dbgs() << "\n";
2127       dbgs() << "ReorderIndices: ";
2128       for (unsigned ReorderIdx : ReorderIndices)
2129         dbgs() << ReorderIdx << ", ";
2130       dbgs() << "\n";
2131       dbgs() << "UserTreeIndices: ";
2132       for (const auto &EInfo : UserTreeIndices)
2133         dbgs() << EInfo << ", ";
2134       dbgs() << "\n";
2135     }
2136 #endif
2137   };
2138 
2139 #ifndef NDEBUG
2140   void dumpTreeCosts(const TreeEntry *E, InstructionCost ReuseShuffleCost,
2141                      InstructionCost VecCost,
2142                      InstructionCost ScalarCost) const {
2143     dbgs() << "SLP: Calculated costs for Tree:\n"; E->dump();
2144     dbgs() << "SLP: Costs:\n";
2145     dbgs() << "SLP:     ReuseShuffleCost = " << ReuseShuffleCost << "\n";
2146     dbgs() << "SLP:     VectorCost = " << VecCost << "\n";
2147     dbgs() << "SLP:     ScalarCost = " << ScalarCost << "\n";
2148     dbgs() << "SLP:     ReuseShuffleCost + VecCost - ScalarCost = " <<
2149                ReuseShuffleCost + VecCost - ScalarCost << "\n";
2150   }
2151 #endif
2152 
2153   /// Create a new VectorizableTree entry.
2154   TreeEntry *newTreeEntry(ArrayRef<Value *> VL, Optional<ScheduleData *> Bundle,
2155                           const InstructionsState &S,
2156                           const EdgeInfo &UserTreeIdx,
2157                           ArrayRef<int> ReuseShuffleIndices = None,
2158                           ArrayRef<unsigned> ReorderIndices = None) {
2159     TreeEntry::EntryState EntryState =
2160         Bundle ? TreeEntry::Vectorize : TreeEntry::NeedToGather;
2161     return newTreeEntry(VL, EntryState, Bundle, S, UserTreeIdx,
2162                         ReuseShuffleIndices, ReorderIndices);
2163   }
2164 
2165   TreeEntry *newTreeEntry(ArrayRef<Value *> VL,
2166                           TreeEntry::EntryState EntryState,
2167                           Optional<ScheduleData *> Bundle,
2168                           const InstructionsState &S,
2169                           const EdgeInfo &UserTreeIdx,
2170                           ArrayRef<int> ReuseShuffleIndices = None,
2171                           ArrayRef<unsigned> ReorderIndices = None) {
2172     assert(((!Bundle && EntryState == TreeEntry::NeedToGather) ||
2173             (Bundle && EntryState != TreeEntry::NeedToGather)) &&
2174            "Need to vectorize gather entry?");
2175     VectorizableTree.push_back(std::make_unique<TreeEntry>(VectorizableTree));
2176     TreeEntry *Last = VectorizableTree.back().get();
2177     Last->Idx = VectorizableTree.size() - 1;
2178     Last->State = EntryState;
2179     Last->ReuseShuffleIndices.append(ReuseShuffleIndices.begin(),
2180                                      ReuseShuffleIndices.end());
2181     if (ReorderIndices.empty()) {
2182       Last->Scalars.assign(VL.begin(), VL.end());
2183       Last->setOperations(S);
2184     } else {
2185       // Reorder scalars and build final mask.
2186       Last->Scalars.assign(VL.size(), nullptr);
2187       transform(ReorderIndices, Last->Scalars.begin(),
2188                 [VL](unsigned Idx) -> Value * {
2189                   if (Idx >= VL.size())
2190                     return UndefValue::get(VL.front()->getType());
2191                   return VL[Idx];
2192                 });
2193       InstructionsState S = getSameOpcode(Last->Scalars);
2194       Last->setOperations(S);
2195       Last->ReorderIndices.append(ReorderIndices.begin(), ReorderIndices.end());
2196     }
2197     if (Last->State != TreeEntry::NeedToGather) {
2198       for (Value *V : VL) {
2199         assert(!getTreeEntry(V) && "Scalar already in tree!");
2200         ScalarToTreeEntry[V] = Last;
2201       }
2202       // Update the scheduler bundle to point to this TreeEntry.
2203       unsigned Lane = 0;
2204       for (ScheduleData *BundleMember = Bundle.getValue(); BundleMember;
2205            BundleMember = BundleMember->NextInBundle) {
2206         BundleMember->TE = Last;
2207         BundleMember->Lane = Lane;
2208         ++Lane;
2209       }
2210       assert((!Bundle.getValue() || Lane == VL.size()) &&
2211              "Bundle and VL out of sync");
2212     } else {
2213       MustGather.insert(VL.begin(), VL.end());
2214     }
2215 
2216     if (UserTreeIdx.UserTE)
2217       Last->UserTreeIndices.push_back(UserTreeIdx);
2218 
2219     return Last;
2220   }
2221 
2222   /// -- Vectorization State --
2223   /// Holds all of the tree entries.
2224   TreeEntry::VecTreeTy VectorizableTree;
2225 
2226 #ifndef NDEBUG
2227   /// Debug printer.
2228   LLVM_DUMP_METHOD void dumpVectorizableTree() const {
2229     for (unsigned Id = 0, IdE = VectorizableTree.size(); Id != IdE; ++Id) {
2230       VectorizableTree[Id]->dump();
2231       dbgs() << "\n";
2232     }
2233   }
2234 #endif
2235 
2236   TreeEntry *getTreeEntry(Value *V) { return ScalarToTreeEntry.lookup(V); }
2237 
2238   const TreeEntry *getTreeEntry(Value *V) const {
2239     return ScalarToTreeEntry.lookup(V);
2240   }
2241 
2242   /// Maps a specific scalar to its tree entry.
2243   SmallDenseMap<Value*, TreeEntry *> ScalarToTreeEntry;
2244 
2245   /// Maps a value to the proposed vectorizable size.
2246   SmallDenseMap<Value *, unsigned> InstrElementSize;
2247 
2248   /// A list of scalars that we found that we need to keep as scalars.
2249   ValueSet MustGather;
2250 
2251   /// This POD struct describes one external user in the vectorized tree.
2252   struct ExternalUser {
2253     ExternalUser(Value *S, llvm::User *U, int L)
2254         : Scalar(S), User(U), Lane(L) {}
2255 
2256     // Which scalar in our function.
2257     Value *Scalar;
2258 
2259     // Which user that uses the scalar.
2260     llvm::User *User;
2261 
2262     // Which lane does the scalar belong to.
2263     int Lane;
2264   };
2265   using UserList = SmallVector<ExternalUser, 16>;
2266 
2267   /// Checks if two instructions may access the same memory.
2268   ///
2269   /// \p Loc1 is the location of \p Inst1. It is passed explicitly because it
2270   /// is invariant in the calling loop.
2271   bool isAliased(const MemoryLocation &Loc1, Instruction *Inst1,
2272                  Instruction *Inst2) {
2273     // First check if the result is already in the cache.
2274     AliasCacheKey key = std::make_pair(Inst1, Inst2);
2275     Optional<bool> &result = AliasCache[key];
2276     if (result.hasValue()) {
2277       return result.getValue();
2278     }
2279     bool aliased = true;
2280     if (Loc1.Ptr && isSimple(Inst1))
2281       aliased = isModOrRefSet(AA->getModRefInfo(Inst2, Loc1));
2282     // Store the result in the cache.
2283     result = aliased;
2284     return aliased;
2285   }
2286 
2287   using AliasCacheKey = std::pair<Instruction *, Instruction *>;
2288 
2289   /// Cache for alias results.
2290   /// TODO: consider moving this to the AliasAnalysis itself.
2291   DenseMap<AliasCacheKey, Optional<bool>> AliasCache;
2292 
2293   /// Removes an instruction from its block and eventually deletes it.
2294   /// It's like Instruction::eraseFromParent() except that the actual deletion
2295   /// is delayed until BoUpSLP is destructed.
2296   /// This is required to ensure that there are no incorrect collisions in the
2297   /// AliasCache, which can happen if a new instruction is allocated at the
2298   /// same address as a previously deleted instruction.
2299   void eraseInstruction(Instruction *I, bool ReplaceOpsWithUndef = false) {
2300     auto It = DeletedInstructions.try_emplace(I, ReplaceOpsWithUndef).first;
2301     It->getSecond() = It->getSecond() && ReplaceOpsWithUndef;
2302   }
2303 
2304   /// Temporary store for deleted instructions. Instructions will be deleted
2305   /// eventually when the BoUpSLP is destructed.
2306   DenseMap<Instruction *, bool> DeletedInstructions;
2307 
2308   /// A list of values that need to extracted out of the tree.
2309   /// This list holds pairs of (Internal Scalar : External User). External User
2310   /// can be nullptr, it means that this Internal Scalar will be used later,
2311   /// after vectorization.
2312   UserList ExternalUses;
2313 
2314   /// Values used only by @llvm.assume calls.
2315   SmallPtrSet<const Value *, 32> EphValues;
2316 
2317   /// Holds all of the instructions that we gathered.
2318   SetVector<Instruction *> GatherShuffleSeq;
2319 
2320   /// A list of blocks that we are going to CSE.
2321   SetVector<BasicBlock *> CSEBlocks;
2322 
2323   /// Contains all scheduling relevant data for an instruction.
2324   /// A ScheduleData either represents a single instruction or a member of an
2325   /// instruction bundle (= a group of instructions which is combined into a
2326   /// vector instruction).
2327   struct ScheduleData {
2328     // The initial value for the dependency counters. It means that the
2329     // dependencies are not calculated yet.
2330     enum { InvalidDeps = -1 };
2331 
2332     ScheduleData() = default;
2333 
2334     void init(int BlockSchedulingRegionID, Value *OpVal) {
2335       FirstInBundle = this;
2336       NextInBundle = nullptr;
2337       NextLoadStore = nullptr;
2338       IsScheduled = false;
2339       SchedulingRegionID = BlockSchedulingRegionID;
2340       UnscheduledDepsInBundle = UnscheduledDeps;
2341       clearDependencies();
2342       OpValue = OpVal;
2343       TE = nullptr;
2344       Lane = -1;
2345     }
2346 
2347     /// Returns true if the dependency information has been calculated.
2348     bool hasValidDependencies() const { return Dependencies != InvalidDeps; }
2349 
2350     /// Returns true for single instructions and for bundle representatives
2351     /// (= the head of a bundle).
2352     bool isSchedulingEntity() const { return FirstInBundle == this; }
2353 
2354     /// Returns true if it represents an instruction bundle and not only a
2355     /// single instruction.
2356     bool isPartOfBundle() const {
2357       return NextInBundle != nullptr || FirstInBundle != this;
2358     }
2359 
2360     /// Returns true if it is ready for scheduling, i.e. it has no more
2361     /// unscheduled depending instructions/bundles.
2362     bool isReady() const {
2363       assert(isSchedulingEntity() &&
2364              "can't consider non-scheduling entity for ready list");
2365       return UnscheduledDepsInBundle == 0 && !IsScheduled;
2366     }
2367 
2368     /// Modifies the number of unscheduled dependencies, also updating it for
2369     /// the whole bundle.
2370     int incrementUnscheduledDeps(int Incr) {
2371       UnscheduledDeps += Incr;
2372       return FirstInBundle->UnscheduledDepsInBundle += Incr;
2373     }
2374 
2375     /// Sets the number of unscheduled dependencies to the number of
2376     /// dependencies.
2377     void resetUnscheduledDeps() {
2378       incrementUnscheduledDeps(Dependencies - UnscheduledDeps);
2379     }
2380 
2381     /// Clears all dependency information.
2382     void clearDependencies() {
2383       Dependencies = InvalidDeps;
2384       resetUnscheduledDeps();
2385       MemoryDependencies.clear();
2386     }
2387 
2388     void dump(raw_ostream &os) const {
2389       if (!isSchedulingEntity()) {
2390         os << "/ " << *Inst;
2391       } else if (NextInBundle) {
2392         os << '[' << *Inst;
2393         ScheduleData *SD = NextInBundle;
2394         while (SD) {
2395           os << ';' << *SD->Inst;
2396           SD = SD->NextInBundle;
2397         }
2398         os << ']';
2399       } else {
2400         os << *Inst;
2401       }
2402     }
2403 
2404     Instruction *Inst = nullptr;
2405 
2406     /// Points to the head in an instruction bundle (and always to this for
2407     /// single instructions).
2408     ScheduleData *FirstInBundle = nullptr;
2409 
2410     /// Single linked list of all instructions in a bundle. Null if it is a
2411     /// single instruction.
2412     ScheduleData *NextInBundle = nullptr;
2413 
2414     /// Single linked list of all memory instructions (e.g. load, store, call)
2415     /// in the block - until the end of the scheduling region.
2416     ScheduleData *NextLoadStore = nullptr;
2417 
2418     /// The dependent memory instructions.
2419     /// This list is derived on demand in calculateDependencies().
2420     SmallVector<ScheduleData *, 4> MemoryDependencies;
2421 
2422     /// This ScheduleData is in the current scheduling region if this matches
2423     /// the current SchedulingRegionID of BlockScheduling.
2424     int SchedulingRegionID = 0;
2425 
2426     /// Used for getting a "good" final ordering of instructions.
2427     int SchedulingPriority = 0;
2428 
2429     /// The number of dependencies. Constitutes of the number of users of the
2430     /// instruction plus the number of dependent memory instructions (if any).
2431     /// This value is calculated on demand.
2432     /// If InvalidDeps, the number of dependencies is not calculated yet.
2433     int Dependencies = InvalidDeps;
2434 
2435     /// The number of dependencies minus the number of dependencies of scheduled
2436     /// instructions. As soon as this is zero, the instruction/bundle gets ready
2437     /// for scheduling.
2438     /// Note that this is negative as long as Dependencies is not calculated.
2439     int UnscheduledDeps = InvalidDeps;
2440 
2441     /// The sum of UnscheduledDeps in a bundle. Equals to UnscheduledDeps for
2442     /// single instructions.
2443     int UnscheduledDepsInBundle = InvalidDeps;
2444 
2445     /// True if this instruction is scheduled (or considered as scheduled in the
2446     /// dry-run).
2447     bool IsScheduled = false;
2448 
2449     /// Opcode of the current instruction in the schedule data.
2450     Value *OpValue = nullptr;
2451 
2452     /// The TreeEntry that this instruction corresponds to.
2453     TreeEntry *TE = nullptr;
2454 
2455     /// The lane of this node in the TreeEntry.
2456     int Lane = -1;
2457   };
2458 
2459 #ifndef NDEBUG
2460   friend inline raw_ostream &operator<<(raw_ostream &os,
2461                                         const BoUpSLP::ScheduleData &SD) {
2462     SD.dump(os);
2463     return os;
2464   }
2465 #endif
2466 
2467   friend struct GraphTraits<BoUpSLP *>;
2468   friend struct DOTGraphTraits<BoUpSLP *>;
2469 
2470   /// Contains all scheduling data for a basic block.
2471   struct BlockScheduling {
2472     BlockScheduling(BasicBlock *BB)
2473         : BB(BB), ChunkSize(BB->size()), ChunkPos(ChunkSize) {}
2474 
2475     void clear() {
2476       ReadyInsts.clear();
2477       ScheduleStart = nullptr;
2478       ScheduleEnd = nullptr;
2479       FirstLoadStoreInRegion = nullptr;
2480       LastLoadStoreInRegion = nullptr;
2481 
2482       // Reduce the maximum schedule region size by the size of the
2483       // previous scheduling run.
2484       ScheduleRegionSizeLimit -= ScheduleRegionSize;
2485       if (ScheduleRegionSizeLimit < MinScheduleRegionSize)
2486         ScheduleRegionSizeLimit = MinScheduleRegionSize;
2487       ScheduleRegionSize = 0;
2488 
2489       // Make a new scheduling region, i.e. all existing ScheduleData is not
2490       // in the new region yet.
2491       ++SchedulingRegionID;
2492     }
2493 
2494     ScheduleData *getScheduleData(Value *V) {
2495       ScheduleData *SD = ScheduleDataMap[V];
2496       if (SD && SD->SchedulingRegionID == SchedulingRegionID)
2497         return SD;
2498       return nullptr;
2499     }
2500 
2501     ScheduleData *getScheduleData(Value *V, Value *Key) {
2502       if (V == Key)
2503         return getScheduleData(V);
2504       auto I = ExtraScheduleDataMap.find(V);
2505       if (I != ExtraScheduleDataMap.end()) {
2506         ScheduleData *SD = I->second[Key];
2507         if (SD && SD->SchedulingRegionID == SchedulingRegionID)
2508           return SD;
2509       }
2510       return nullptr;
2511     }
2512 
2513     bool isInSchedulingRegion(ScheduleData *SD) const {
2514       return SD->SchedulingRegionID == SchedulingRegionID;
2515     }
2516 
2517     /// Marks an instruction as scheduled and puts all dependent ready
2518     /// instructions into the ready-list.
2519     template <typename ReadyListType>
2520     void schedule(ScheduleData *SD, ReadyListType &ReadyList) {
2521       SD->IsScheduled = true;
2522       LLVM_DEBUG(dbgs() << "SLP:   schedule " << *SD << "\n");
2523 
2524       ScheduleData *BundleMember = SD;
2525       while (BundleMember) {
2526         if (BundleMember->Inst != BundleMember->OpValue) {
2527           BundleMember = BundleMember->NextInBundle;
2528           continue;
2529         }
2530         // Handle the def-use chain dependencies.
2531 
2532         // Decrement the unscheduled counter and insert to ready list if ready.
2533         auto &&DecrUnsched = [this, &ReadyList](Instruction *I) {
2534           doForAllOpcodes(I, [&ReadyList](ScheduleData *OpDef) {
2535             if (OpDef && OpDef->hasValidDependencies() &&
2536                 OpDef->incrementUnscheduledDeps(-1) == 0) {
2537               // There are no more unscheduled dependencies after
2538               // decrementing, so we can put the dependent instruction
2539               // into the ready list.
2540               ScheduleData *DepBundle = OpDef->FirstInBundle;
2541               assert(!DepBundle->IsScheduled &&
2542                      "already scheduled bundle gets ready");
2543               ReadyList.insert(DepBundle);
2544               LLVM_DEBUG(dbgs()
2545                          << "SLP:    gets ready (def): " << *DepBundle << "\n");
2546             }
2547           });
2548         };
2549 
2550         // If BundleMember is a vector bundle, its operands may have been
2551         // reordered duiring buildTree(). We therefore need to get its operands
2552         // through the TreeEntry.
2553         if (TreeEntry *TE = BundleMember->TE) {
2554           int Lane = BundleMember->Lane;
2555           assert(Lane >= 0 && "Lane not set");
2556 
2557           // Since vectorization tree is being built recursively this assertion
2558           // ensures that the tree entry has all operands set before reaching
2559           // this code. Couple of exceptions known at the moment are extracts
2560           // where their second (immediate) operand is not added. Since
2561           // immediates do not affect scheduler behavior this is considered
2562           // okay.
2563           auto *In = TE->getMainOp();
2564           assert(In &&
2565                  (isa<ExtractValueInst>(In) || isa<ExtractElementInst>(In) ||
2566                   In->getNumOperands() == TE->getNumOperands()) &&
2567                  "Missed TreeEntry operands?");
2568           (void)In; // fake use to avoid build failure when assertions disabled
2569 
2570           for (unsigned OpIdx = 0, NumOperands = TE->getNumOperands();
2571                OpIdx != NumOperands; ++OpIdx)
2572             if (auto *I = dyn_cast<Instruction>(TE->getOperand(OpIdx)[Lane]))
2573               DecrUnsched(I);
2574         } else {
2575           // If BundleMember is a stand-alone instruction, no operand reordering
2576           // has taken place, so we directly access its operands.
2577           for (Use &U : BundleMember->Inst->operands())
2578             if (auto *I = dyn_cast<Instruction>(U.get()))
2579               DecrUnsched(I);
2580         }
2581         // Handle the memory dependencies.
2582         for (ScheduleData *MemoryDepSD : BundleMember->MemoryDependencies) {
2583           if (MemoryDepSD->incrementUnscheduledDeps(-1) == 0) {
2584             // There are no more unscheduled dependencies after decrementing,
2585             // so we can put the dependent instruction into the ready list.
2586             ScheduleData *DepBundle = MemoryDepSD->FirstInBundle;
2587             assert(!DepBundle->IsScheduled &&
2588                    "already scheduled bundle gets ready");
2589             ReadyList.insert(DepBundle);
2590             LLVM_DEBUG(dbgs()
2591                        << "SLP:    gets ready (mem): " << *DepBundle << "\n");
2592           }
2593         }
2594         BundleMember = BundleMember->NextInBundle;
2595       }
2596     }
2597 
2598     void doForAllOpcodes(Value *V,
2599                          function_ref<void(ScheduleData *SD)> Action) {
2600       if (ScheduleData *SD = getScheduleData(V))
2601         Action(SD);
2602       auto I = ExtraScheduleDataMap.find(V);
2603       if (I != ExtraScheduleDataMap.end())
2604         for (auto &P : I->second)
2605           if (P.second->SchedulingRegionID == SchedulingRegionID)
2606             Action(P.second);
2607     }
2608 
2609     /// Put all instructions into the ReadyList which are ready for scheduling.
2610     template <typename ReadyListType>
2611     void initialFillReadyList(ReadyListType &ReadyList) {
2612       for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
2613         doForAllOpcodes(I, [&](ScheduleData *SD) {
2614           if (SD->isSchedulingEntity() && SD->isReady()) {
2615             ReadyList.insert(SD);
2616             LLVM_DEBUG(dbgs()
2617                        << "SLP:    initially in ready list: " << *I << "\n");
2618           }
2619         });
2620       }
2621     }
2622 
2623     /// Checks if a bundle of instructions can be scheduled, i.e. has no
2624     /// cyclic dependencies. This is only a dry-run, no instructions are
2625     /// actually moved at this stage.
2626     /// \returns the scheduling bundle. The returned Optional value is non-None
2627     /// if \p VL is allowed to be scheduled.
2628     Optional<ScheduleData *>
2629     tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
2630                       const InstructionsState &S);
2631 
2632     /// Un-bundles a group of instructions.
2633     void cancelScheduling(ArrayRef<Value *> VL, Value *OpValue);
2634 
2635     /// Allocates schedule data chunk.
2636     ScheduleData *allocateScheduleDataChunks();
2637 
2638     /// Extends the scheduling region so that V is inside the region.
2639     /// \returns true if the region size is within the limit.
2640     bool extendSchedulingRegion(Value *V, const InstructionsState &S);
2641 
2642     /// Initialize the ScheduleData structures for new instructions in the
2643     /// scheduling region.
2644     void initScheduleData(Instruction *FromI, Instruction *ToI,
2645                           ScheduleData *PrevLoadStore,
2646                           ScheduleData *NextLoadStore);
2647 
2648     /// Updates the dependency information of a bundle and of all instructions/
2649     /// bundles which depend on the original bundle.
2650     void calculateDependencies(ScheduleData *SD, bool InsertInReadyList,
2651                                BoUpSLP *SLP);
2652 
2653     /// Sets all instruction in the scheduling region to un-scheduled.
2654     void resetSchedule();
2655 
2656     BasicBlock *BB;
2657 
2658     /// Simple memory allocation for ScheduleData.
2659     std::vector<std::unique_ptr<ScheduleData[]>> ScheduleDataChunks;
2660 
2661     /// The size of a ScheduleData array in ScheduleDataChunks.
2662     int ChunkSize;
2663 
2664     /// The allocator position in the current chunk, which is the last entry
2665     /// of ScheduleDataChunks.
2666     int ChunkPos;
2667 
2668     /// Attaches ScheduleData to Instruction.
2669     /// Note that the mapping survives during all vectorization iterations, i.e.
2670     /// ScheduleData structures are recycled.
2671     DenseMap<Value *, ScheduleData *> ScheduleDataMap;
2672 
2673     /// Attaches ScheduleData to Instruction with the leading key.
2674     DenseMap<Value *, SmallDenseMap<Value *, ScheduleData *>>
2675         ExtraScheduleDataMap;
2676 
2677     struct ReadyList : SmallVector<ScheduleData *, 8> {
2678       void insert(ScheduleData *SD) { push_back(SD); }
2679     };
2680 
2681     /// The ready-list for scheduling (only used for the dry-run).
2682     ReadyList ReadyInsts;
2683 
2684     /// The first instruction of the scheduling region.
2685     Instruction *ScheduleStart = nullptr;
2686 
2687     /// The first instruction _after_ the scheduling region.
2688     Instruction *ScheduleEnd = nullptr;
2689 
2690     /// The first memory accessing instruction in the scheduling region
2691     /// (can be null).
2692     ScheduleData *FirstLoadStoreInRegion = nullptr;
2693 
2694     /// The last memory accessing instruction in the scheduling region
2695     /// (can be null).
2696     ScheduleData *LastLoadStoreInRegion = nullptr;
2697 
2698     /// The current size of the scheduling region.
2699     int ScheduleRegionSize = 0;
2700 
2701     /// The maximum size allowed for the scheduling region.
2702     int ScheduleRegionSizeLimit = ScheduleRegionSizeBudget;
2703 
2704     /// The ID of the scheduling region. For a new vectorization iteration this
2705     /// is incremented which "removes" all ScheduleData from the region.
2706     // Make sure that the initial SchedulingRegionID is greater than the
2707     // initial SchedulingRegionID in ScheduleData (which is 0).
2708     int SchedulingRegionID = 1;
2709   };
2710 
2711   /// Attaches the BlockScheduling structures to basic blocks.
2712   MapVector<BasicBlock *, std::unique_ptr<BlockScheduling>> BlocksSchedules;
2713 
2714   /// Performs the "real" scheduling. Done before vectorization is actually
2715   /// performed in a basic block.
2716   void scheduleBlock(BlockScheduling *BS);
2717 
2718   /// List of users to ignore during scheduling and that don't need extracting.
2719   ArrayRef<Value *> UserIgnoreList;
2720 
2721   /// A DenseMapInfo implementation for holding DenseMaps and DenseSets of
2722   /// sorted SmallVectors of unsigned.
2723   struct OrdersTypeDenseMapInfo {
2724     static OrdersType getEmptyKey() {
2725       OrdersType V;
2726       V.push_back(~1U);
2727       return V;
2728     }
2729 
2730     static OrdersType getTombstoneKey() {
2731       OrdersType V;
2732       V.push_back(~2U);
2733       return V;
2734     }
2735 
2736     static unsigned getHashValue(const OrdersType &V) {
2737       return static_cast<unsigned>(hash_combine_range(V.begin(), V.end()));
2738     }
2739 
2740     static bool isEqual(const OrdersType &LHS, const OrdersType &RHS) {
2741       return LHS == RHS;
2742     }
2743   };
2744 
2745   // Analysis and block reference.
2746   Function *F;
2747   ScalarEvolution *SE;
2748   TargetTransformInfo *TTI;
2749   TargetLibraryInfo *TLI;
2750   AAResults *AA;
2751   LoopInfo *LI;
2752   DominatorTree *DT;
2753   AssumptionCache *AC;
2754   DemandedBits *DB;
2755   const DataLayout *DL;
2756   OptimizationRemarkEmitter *ORE;
2757 
2758   unsigned MaxVecRegSize; // This is set by TTI or overridden by cl::opt.
2759   unsigned MinVecRegSize; // Set by cl::opt (default: 128).
2760 
2761   /// Instruction builder to construct the vectorized tree.
2762   IRBuilder<> Builder;
2763 
2764   /// A map of scalar integer values to the smallest bit width with which they
2765   /// can legally be represented. The values map to (width, signed) pairs,
2766   /// where "width" indicates the minimum bit width and "signed" is True if the
2767   /// value must be signed-extended, rather than zero-extended, back to its
2768   /// original width.
2769   MapVector<Value *, std::pair<uint64_t, bool>> MinBWs;
2770 };
2771 
2772 } // end namespace slpvectorizer
2773 
2774 template <> struct GraphTraits<BoUpSLP *> {
2775   using TreeEntry = BoUpSLP::TreeEntry;
2776 
2777   /// NodeRef has to be a pointer per the GraphWriter.
2778   using NodeRef = TreeEntry *;
2779 
2780   using ContainerTy = BoUpSLP::TreeEntry::VecTreeTy;
2781 
2782   /// Add the VectorizableTree to the index iterator to be able to return
2783   /// TreeEntry pointers.
2784   struct ChildIteratorType
2785       : public iterator_adaptor_base<
2786             ChildIteratorType, SmallVector<BoUpSLP::EdgeInfo, 1>::iterator> {
2787     ContainerTy &VectorizableTree;
2788 
2789     ChildIteratorType(SmallVector<BoUpSLP::EdgeInfo, 1>::iterator W,
2790                       ContainerTy &VT)
2791         : ChildIteratorType::iterator_adaptor_base(W), VectorizableTree(VT) {}
2792 
2793     NodeRef operator*() { return I->UserTE; }
2794   };
2795 
2796   static NodeRef getEntryNode(BoUpSLP &R) {
2797     return R.VectorizableTree[0].get();
2798   }
2799 
2800   static ChildIteratorType child_begin(NodeRef N) {
2801     return {N->UserTreeIndices.begin(), N->Container};
2802   }
2803 
2804   static ChildIteratorType child_end(NodeRef N) {
2805     return {N->UserTreeIndices.end(), N->Container};
2806   }
2807 
2808   /// For the node iterator we just need to turn the TreeEntry iterator into a
2809   /// TreeEntry* iterator so that it dereferences to NodeRef.
2810   class nodes_iterator {
2811     using ItTy = ContainerTy::iterator;
2812     ItTy It;
2813 
2814   public:
2815     nodes_iterator(const ItTy &It2) : It(It2) {}
2816     NodeRef operator*() { return It->get(); }
2817     nodes_iterator operator++() {
2818       ++It;
2819       return *this;
2820     }
2821     bool operator!=(const nodes_iterator &N2) const { return N2.It != It; }
2822   };
2823 
2824   static nodes_iterator nodes_begin(BoUpSLP *R) {
2825     return nodes_iterator(R->VectorizableTree.begin());
2826   }
2827 
2828   static nodes_iterator nodes_end(BoUpSLP *R) {
2829     return nodes_iterator(R->VectorizableTree.end());
2830   }
2831 
2832   static unsigned size(BoUpSLP *R) { return R->VectorizableTree.size(); }
2833 };
2834 
2835 template <> struct DOTGraphTraits<BoUpSLP *> : public DefaultDOTGraphTraits {
2836   using TreeEntry = BoUpSLP::TreeEntry;
2837 
2838   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
2839 
2840   std::string getNodeLabel(const TreeEntry *Entry, const BoUpSLP *R) {
2841     std::string Str;
2842     raw_string_ostream OS(Str);
2843     if (isSplat(Entry->Scalars))
2844       OS << "<splat> ";
2845     for (auto V : Entry->Scalars) {
2846       OS << *V;
2847       if (llvm::any_of(R->ExternalUses, [&](const BoUpSLP::ExternalUser &EU) {
2848             return EU.Scalar == V;
2849           }))
2850         OS << " <extract>";
2851       OS << "\n";
2852     }
2853     return Str;
2854   }
2855 
2856   static std::string getNodeAttributes(const TreeEntry *Entry,
2857                                        const BoUpSLP *) {
2858     if (Entry->State == TreeEntry::NeedToGather)
2859       return "color=red";
2860     return "";
2861   }
2862 };
2863 
2864 } // end namespace llvm
2865 
2866 BoUpSLP::~BoUpSLP() {
2867   for (const auto &Pair : DeletedInstructions) {
2868     // Replace operands of ignored instructions with Undefs in case if they were
2869     // marked for deletion.
2870     if (Pair.getSecond()) {
2871       Value *Undef = UndefValue::get(Pair.getFirst()->getType());
2872       Pair.getFirst()->replaceAllUsesWith(Undef);
2873     }
2874     Pair.getFirst()->dropAllReferences();
2875   }
2876   for (const auto &Pair : DeletedInstructions) {
2877     assert(Pair.getFirst()->use_empty() &&
2878            "trying to erase instruction with users.");
2879     Pair.getFirst()->eraseFromParent();
2880   }
2881 #ifdef EXPENSIVE_CHECKS
2882   // If we could guarantee that this call is not extremely slow, we could
2883   // remove the ifdef limitation (see PR47712).
2884   assert(!verifyFunction(*F, &dbgs()));
2885 #endif
2886 }
2887 
2888 void BoUpSLP::eraseInstructions(ArrayRef<Value *> AV) {
2889   for (auto *V : AV) {
2890     if (auto *I = dyn_cast<Instruction>(V))
2891       eraseInstruction(I, /*ReplaceOpsWithUndef=*/true);
2892   };
2893 }
2894 
2895 /// Reorders the given \p Reuses mask according to the given \p Mask. \p Reuses
2896 /// contains original mask for the scalars reused in the node. Procedure
2897 /// transform this mask in accordance with the given \p Mask.
2898 static void reorderReuses(SmallVectorImpl<int> &Reuses, ArrayRef<int> Mask) {
2899   assert(!Mask.empty() && Reuses.size() == Mask.size() &&
2900          "Expected non-empty mask.");
2901   SmallVector<int> Prev(Reuses.begin(), Reuses.end());
2902   Prev.swap(Reuses);
2903   for (unsigned I = 0, E = Prev.size(); I < E; ++I)
2904     if (Mask[I] != UndefMaskElem)
2905       Reuses[Mask[I]] = Prev[I];
2906 }
2907 
2908 /// Reorders the given \p Order according to the given \p Mask. \p Order - is
2909 /// the original order of the scalars. Procedure transforms the provided order
2910 /// in accordance with the given \p Mask. If the resulting \p Order is just an
2911 /// identity order, \p Order is cleared.
2912 static void reorderOrder(SmallVectorImpl<unsigned> &Order, ArrayRef<int> Mask) {
2913   assert(!Mask.empty() && "Expected non-empty mask.");
2914   SmallVector<int> MaskOrder;
2915   if (Order.empty()) {
2916     MaskOrder.resize(Mask.size());
2917     std::iota(MaskOrder.begin(), MaskOrder.end(), 0);
2918   } else {
2919     inversePermutation(Order, MaskOrder);
2920   }
2921   reorderReuses(MaskOrder, Mask);
2922   if (ShuffleVectorInst::isIdentityMask(MaskOrder)) {
2923     Order.clear();
2924     return;
2925   }
2926   Order.assign(Mask.size(), Mask.size());
2927   for (unsigned I = 0, E = Mask.size(); I < E; ++I)
2928     if (MaskOrder[I] != UndefMaskElem)
2929       Order[MaskOrder[I]] = I;
2930   fixupOrderingIndices(Order);
2931 }
2932 
2933 Optional<BoUpSLP::OrdersType>
2934 BoUpSLP::findReusedOrderedScalars(const BoUpSLP::TreeEntry &TE) {
2935   assert(TE.State == TreeEntry::NeedToGather && "Expected gather node only.");
2936   unsigned NumScalars = TE.Scalars.size();
2937   OrdersType CurrentOrder(NumScalars, NumScalars);
2938   SmallVector<int> Positions;
2939   SmallBitVector UsedPositions(NumScalars);
2940   const TreeEntry *STE = nullptr;
2941   // Try to find all gathered scalars that are gets vectorized in other
2942   // vectorize node. Here we can have only one single tree vector node to
2943   // correctly identify order of the gathered scalars.
2944   for (unsigned I = 0; I < NumScalars; ++I) {
2945     Value *V = TE.Scalars[I];
2946     if (!isa<LoadInst, ExtractElementInst, ExtractValueInst>(V))
2947       continue;
2948     if (const auto *LocalSTE = getTreeEntry(V)) {
2949       if (!STE)
2950         STE = LocalSTE;
2951       else if (STE != LocalSTE)
2952         // Take the order only from the single vector node.
2953         return None;
2954       unsigned Lane =
2955           std::distance(STE->Scalars.begin(), find(STE->Scalars, V));
2956       if (Lane >= NumScalars)
2957         return None;
2958       if (CurrentOrder[Lane] != NumScalars) {
2959         if (Lane != I)
2960           continue;
2961         UsedPositions.reset(CurrentOrder[Lane]);
2962       }
2963       // The partial identity (where only some elements of the gather node are
2964       // in the identity order) is good.
2965       CurrentOrder[Lane] = I;
2966       UsedPositions.set(I);
2967     }
2968   }
2969   // Need to keep the order if we have a vector entry and at least 2 scalars or
2970   // the vectorized entry has just 2 scalars.
2971   if (STE && (UsedPositions.count() > 1 || STE->Scalars.size() == 2)) {
2972     auto &&IsIdentityOrder = [NumScalars](ArrayRef<unsigned> CurrentOrder) {
2973       for (unsigned I = 0; I < NumScalars; ++I)
2974         if (CurrentOrder[I] != I && CurrentOrder[I] != NumScalars)
2975           return false;
2976       return true;
2977     };
2978     if (IsIdentityOrder(CurrentOrder)) {
2979       CurrentOrder.clear();
2980       return CurrentOrder;
2981     }
2982     auto *It = CurrentOrder.begin();
2983     for (unsigned I = 0; I < NumScalars;) {
2984       if (UsedPositions.test(I)) {
2985         ++I;
2986         continue;
2987       }
2988       if (*It == NumScalars) {
2989         *It = I;
2990         ++I;
2991       }
2992       ++It;
2993     }
2994     return CurrentOrder;
2995   }
2996   return None;
2997 }
2998 
2999 Optional<BoUpSLP::OrdersType> BoUpSLP::getReorderingData(const TreeEntry &TE,
3000                                                          bool TopToBottom) {
3001   // No need to reorder if need to shuffle reuses, still need to shuffle the
3002   // node.
3003   if (!TE.ReuseShuffleIndices.empty())
3004     return None;
3005   if (TE.State == TreeEntry::Vectorize &&
3006       (isa<LoadInst, ExtractElementInst, ExtractValueInst>(TE.getMainOp()) ||
3007        (TopToBottom && isa<StoreInst, InsertElementInst>(TE.getMainOp()))) &&
3008       !TE.isAltShuffle())
3009     return TE.ReorderIndices;
3010   if (TE.State == TreeEntry::NeedToGather) {
3011     // TODO: add analysis of other gather nodes with extractelement
3012     // instructions and other values/instructions, not only undefs.
3013     if (((TE.getOpcode() == Instruction::ExtractElement &&
3014           !TE.isAltShuffle()) ||
3015          (all_of(TE.Scalars,
3016                  [](Value *V) {
3017                    return isa<UndefValue, ExtractElementInst>(V);
3018                  }) &&
3019           any_of(TE.Scalars,
3020                  [](Value *V) { return isa<ExtractElementInst>(V); }))) &&
3021         all_of(TE.Scalars,
3022                [](Value *V) {
3023                  auto *EE = dyn_cast<ExtractElementInst>(V);
3024                  return !EE || isa<FixedVectorType>(EE->getVectorOperandType());
3025                }) &&
3026         allSameType(TE.Scalars)) {
3027       // Check that gather of extractelements can be represented as
3028       // just a shuffle of a single vector.
3029       OrdersType CurrentOrder;
3030       bool Reuse = canReuseExtract(TE.Scalars, TE.getMainOp(), CurrentOrder);
3031       if (Reuse || !CurrentOrder.empty()) {
3032         if (!CurrentOrder.empty())
3033           fixupOrderingIndices(CurrentOrder);
3034         return CurrentOrder;
3035       }
3036     }
3037     if (Optional<OrdersType> CurrentOrder = findReusedOrderedScalars(TE))
3038       return CurrentOrder;
3039   }
3040   return None;
3041 }
3042 
3043 void BoUpSLP::reorderTopToBottom() {
3044   // Maps VF to the graph nodes.
3045   DenseMap<unsigned, SetVector<TreeEntry *>> VFToOrderedEntries;
3046   // ExtractElement gather nodes which can be vectorized and need to handle
3047   // their ordering.
3048   DenseMap<const TreeEntry *, OrdersType> GathersToOrders;
3049   // Find all reorderable nodes with the given VF.
3050   // Currently the are vectorized stores,loads,extracts + some gathering of
3051   // extracts.
3052   for_each(VectorizableTree, [this, &VFToOrderedEntries, &GathersToOrders](
3053                                  const std::unique_ptr<TreeEntry> &TE) {
3054     if (Optional<OrdersType> CurrentOrder =
3055             getReorderingData(*TE.get(), /*TopToBottom=*/true)) {
3056       // Do not include ordering for nodes used in the alt opcode vectorization,
3057       // better to reorder them during bottom-to-top stage. If follow the order
3058       // here, it causes reordering of the whole graph though actually it is
3059       // profitable just to reorder the subgraph that starts from the alternate
3060       // opcode vectorization node. Such nodes already end-up with the shuffle
3061       // instruction and it is just enough to change this shuffle rather than
3062       // rotate the scalars for the whole graph.
3063       unsigned Cnt = 0;
3064       const TreeEntry *UserTE = TE.get();
3065       while (UserTE && Cnt < RecursionMaxDepth) {
3066         if (UserTE->UserTreeIndices.size() != 1)
3067           break;
3068         if (all_of(UserTE->UserTreeIndices, [](const EdgeInfo &EI) {
3069               return EI.UserTE->State == TreeEntry::Vectorize &&
3070                      EI.UserTE->isAltShuffle() && EI.UserTE->Idx != 0;
3071             }))
3072           return;
3073         if (UserTE->UserTreeIndices.empty())
3074           UserTE = nullptr;
3075         else
3076           UserTE = UserTE->UserTreeIndices.back().UserTE;
3077         ++Cnt;
3078       }
3079       VFToOrderedEntries[TE->Scalars.size()].insert(TE.get());
3080       if (TE->State != TreeEntry::Vectorize)
3081         GathersToOrders.try_emplace(TE.get(), *CurrentOrder);
3082     }
3083   });
3084 
3085   // Reorder the graph nodes according to their vectorization factor.
3086   for (unsigned VF = VectorizableTree.front()->Scalars.size(); VF > 1;
3087        VF /= 2) {
3088     auto It = VFToOrderedEntries.find(VF);
3089     if (It == VFToOrderedEntries.end())
3090       continue;
3091     // Try to find the most profitable order. We just are looking for the most
3092     // used order and reorder scalar elements in the nodes according to this
3093     // mostly used order.
3094     ArrayRef<TreeEntry *> OrderedEntries = It->second.getArrayRef();
3095     // All operands are reordered and used only in this node - propagate the
3096     // most used order to the user node.
3097     MapVector<OrdersType, unsigned,
3098               DenseMap<OrdersType, unsigned, OrdersTypeDenseMapInfo>>
3099         OrdersUses;
3100     SmallPtrSet<const TreeEntry *, 4> VisitedOps;
3101     for (const TreeEntry *OpTE : OrderedEntries) {
3102       // No need to reorder this nodes, still need to extend and to use shuffle,
3103       // just need to merge reordering shuffle and the reuse shuffle.
3104       if (!OpTE->ReuseShuffleIndices.empty())
3105         continue;
3106       // Count number of orders uses.
3107       const auto &Order = [OpTE, &GathersToOrders]() -> const OrdersType & {
3108         if (OpTE->State == TreeEntry::NeedToGather)
3109           return GathersToOrders.find(OpTE)->second;
3110         return OpTE->ReorderIndices;
3111       }();
3112       // Stores actually store the mask, not the order, need to invert.
3113       if (OpTE->State == TreeEntry::Vectorize && !OpTE->isAltShuffle() &&
3114           OpTE->getOpcode() == Instruction::Store && !Order.empty()) {
3115         SmallVector<int> Mask;
3116         inversePermutation(Order, Mask);
3117         unsigned E = Order.size();
3118         OrdersType CurrentOrder(E, E);
3119         transform(Mask, CurrentOrder.begin(), [E](int Idx) {
3120           return Idx == UndefMaskElem ? E : static_cast<unsigned>(Idx);
3121         });
3122         fixupOrderingIndices(CurrentOrder);
3123         ++OrdersUses.insert(std::make_pair(CurrentOrder, 0)).first->second;
3124       } else {
3125         ++OrdersUses.insert(std::make_pair(Order, 0)).first->second;
3126       }
3127     }
3128     // Set order of the user node.
3129     if (OrdersUses.empty())
3130       continue;
3131     // Choose the most used order.
3132     ArrayRef<unsigned> BestOrder = OrdersUses.front().first;
3133     unsigned Cnt = OrdersUses.front().second;
3134     for (const auto &Pair : drop_begin(OrdersUses)) {
3135       if (Cnt < Pair.second || (Cnt == Pair.second && Pair.first.empty())) {
3136         BestOrder = Pair.first;
3137         Cnt = Pair.second;
3138       }
3139     }
3140     // Set order of the user node.
3141     if (BestOrder.empty())
3142       continue;
3143     SmallVector<int> Mask;
3144     inversePermutation(BestOrder, Mask);
3145     SmallVector<int> MaskOrder(BestOrder.size(), UndefMaskElem);
3146     unsigned E = BestOrder.size();
3147     transform(BestOrder, MaskOrder.begin(), [E](unsigned I) {
3148       return I < E ? static_cast<int>(I) : UndefMaskElem;
3149     });
3150     // Do an actual reordering, if profitable.
3151     for (std::unique_ptr<TreeEntry> &TE : VectorizableTree) {
3152       // Just do the reordering for the nodes with the given VF.
3153       if (TE->Scalars.size() != VF) {
3154         if (TE->ReuseShuffleIndices.size() == VF) {
3155           // Need to reorder the reuses masks of the operands with smaller VF to
3156           // be able to find the match between the graph nodes and scalar
3157           // operands of the given node during vectorization/cost estimation.
3158           assert(all_of(TE->UserTreeIndices,
3159                         [VF, &TE](const EdgeInfo &EI) {
3160                           return EI.UserTE->Scalars.size() == VF ||
3161                                  EI.UserTE->Scalars.size() ==
3162                                      TE->Scalars.size();
3163                         }) &&
3164                  "All users must be of VF size.");
3165           // Update ordering of the operands with the smaller VF than the given
3166           // one.
3167           reorderReuses(TE->ReuseShuffleIndices, Mask);
3168         }
3169         continue;
3170       }
3171       if (TE->State == TreeEntry::Vectorize &&
3172           isa<ExtractElementInst, ExtractValueInst, LoadInst, StoreInst,
3173               InsertElementInst>(TE->getMainOp()) &&
3174           !TE->isAltShuffle()) {
3175         // Build correct orders for extract{element,value}, loads and
3176         // stores.
3177         reorderOrder(TE->ReorderIndices, Mask);
3178         if (isa<InsertElementInst, StoreInst>(TE->getMainOp()))
3179           TE->reorderOperands(Mask);
3180       } else {
3181         // Reorder the node and its operands.
3182         TE->reorderOperands(Mask);
3183         assert(TE->ReorderIndices.empty() &&
3184                "Expected empty reorder sequence.");
3185         reorderScalars(TE->Scalars, Mask);
3186       }
3187       if (!TE->ReuseShuffleIndices.empty()) {
3188         // Apply reversed order to keep the original ordering of the reused
3189         // elements to avoid extra reorder indices shuffling.
3190         OrdersType CurrentOrder;
3191         reorderOrder(CurrentOrder, MaskOrder);
3192         SmallVector<int> NewReuses;
3193         inversePermutation(CurrentOrder, NewReuses);
3194         addMask(NewReuses, TE->ReuseShuffleIndices);
3195         TE->ReuseShuffleIndices.swap(NewReuses);
3196       }
3197     }
3198   }
3199 }
3200 
3201 void BoUpSLP::reorderBottomToTop(bool IgnoreReorder) {
3202   SetVector<TreeEntry *> OrderedEntries;
3203   DenseMap<const TreeEntry *, OrdersType> GathersToOrders;
3204   // Find all reorderable leaf nodes with the given VF.
3205   // Currently the are vectorized loads,extracts without alternate operands +
3206   // some gathering of extracts.
3207   SmallVector<TreeEntry *> NonVectorized;
3208   for_each(VectorizableTree, [this, &OrderedEntries, &GathersToOrders,
3209                               &NonVectorized](
3210                                  const std::unique_ptr<TreeEntry> &TE) {
3211     if (TE->State != TreeEntry::Vectorize)
3212       NonVectorized.push_back(TE.get());
3213     if (Optional<OrdersType> CurrentOrder =
3214             getReorderingData(*TE.get(), /*TopToBottom=*/false)) {
3215       OrderedEntries.insert(TE.get());
3216       if (TE->State != TreeEntry::Vectorize)
3217         GathersToOrders.try_emplace(TE.get(), *CurrentOrder);
3218     }
3219   });
3220 
3221   // Checks if the operands of the users are reordarable and have only single
3222   // use.
3223   auto &&CheckOperands =
3224       [this, &NonVectorized](const auto &Data,
3225                              SmallVectorImpl<TreeEntry *> &GatherOps) {
3226         for (unsigned I = 0, E = Data.first->getNumOperands(); I < E; ++I) {
3227           if (any_of(Data.second,
3228                      [I](const std::pair<unsigned, TreeEntry *> &OpData) {
3229                        return OpData.first == I &&
3230                               OpData.second->State == TreeEntry::Vectorize;
3231                      }))
3232             continue;
3233           ArrayRef<Value *> VL = Data.first->getOperand(I);
3234           const TreeEntry *TE = nullptr;
3235           const auto *It = find_if(VL, [this, &TE](Value *V) {
3236             TE = getTreeEntry(V);
3237             return TE;
3238           });
3239           if (It != VL.end() && TE->isSame(VL))
3240             return false;
3241           TreeEntry *Gather = nullptr;
3242           if (count_if(NonVectorized, [VL, &Gather](TreeEntry *TE) {
3243                 assert(TE->State != TreeEntry::Vectorize &&
3244                        "Only non-vectorized nodes are expected.");
3245                 if (TE->isSame(VL)) {
3246                   Gather = TE;
3247                   return true;
3248                 }
3249                 return false;
3250               }) > 1)
3251             return false;
3252           if (Gather)
3253             GatherOps.push_back(Gather);
3254         }
3255         return true;
3256       };
3257   // 1. Propagate order to the graph nodes, which use only reordered nodes.
3258   // I.e., if the node has operands, that are reordered, try to make at least
3259   // one operand order in the natural order and reorder others + reorder the
3260   // user node itself.
3261   SmallPtrSet<const TreeEntry *, 4> Visited;
3262   while (!OrderedEntries.empty()) {
3263     // 1. Filter out only reordered nodes.
3264     // 2. If the entry has multiple uses - skip it and jump to the next node.
3265     MapVector<TreeEntry *, SmallVector<std::pair<unsigned, TreeEntry *>>> Users;
3266     SmallVector<TreeEntry *> Filtered;
3267     for (TreeEntry *TE : OrderedEntries) {
3268       if (!(TE->State == TreeEntry::Vectorize ||
3269             (TE->State == TreeEntry::NeedToGather &&
3270              GathersToOrders.count(TE))) ||
3271           TE->UserTreeIndices.empty() || !TE->ReuseShuffleIndices.empty() ||
3272           !all_of(drop_begin(TE->UserTreeIndices),
3273                   [TE](const EdgeInfo &EI) {
3274                     return EI.UserTE == TE->UserTreeIndices.front().UserTE;
3275                   }) ||
3276           !Visited.insert(TE).second) {
3277         Filtered.push_back(TE);
3278         continue;
3279       }
3280       // Build a map between user nodes and their operands order to speedup
3281       // search. The graph currently does not provide this dependency directly.
3282       for (EdgeInfo &EI : TE->UserTreeIndices) {
3283         TreeEntry *UserTE = EI.UserTE;
3284         auto It = Users.find(UserTE);
3285         if (It == Users.end())
3286           It = Users.insert({UserTE, {}}).first;
3287         It->second.emplace_back(EI.EdgeIdx, TE);
3288       }
3289     }
3290     // Erase filtered entries.
3291     for_each(Filtered,
3292              [&OrderedEntries](TreeEntry *TE) { OrderedEntries.remove(TE); });
3293     for (const auto &Data : Users) {
3294       // Check that operands are used only in the User node.
3295       SmallVector<TreeEntry *> GatherOps;
3296       if (!CheckOperands(Data, GatherOps)) {
3297         for_each(Data.second,
3298                  [&OrderedEntries](const std::pair<unsigned, TreeEntry *> &Op) {
3299                    OrderedEntries.remove(Op.second);
3300                  });
3301         continue;
3302       }
3303       // All operands are reordered and used only in this node - propagate the
3304       // most used order to the user node.
3305       MapVector<OrdersType, unsigned,
3306                 DenseMap<OrdersType, unsigned, OrdersTypeDenseMapInfo>>
3307           OrdersUses;
3308       SmallPtrSet<const TreeEntry *, 4> VisitedOps;
3309       for (const auto &Op : Data.second) {
3310         TreeEntry *OpTE = Op.second;
3311         if (!OpTE->ReuseShuffleIndices.empty() ||
3312             (IgnoreReorder && OpTE == VectorizableTree.front().get()))
3313           continue;
3314         const auto &Order = [OpTE, &GathersToOrders]() -> const OrdersType & {
3315           if (OpTE->State == TreeEntry::NeedToGather)
3316             return GathersToOrders.find(OpTE)->second;
3317           return OpTE->ReorderIndices;
3318         }();
3319         // Stores actually store the mask, not the order, need to invert.
3320         if (OpTE->State == TreeEntry::Vectorize && !OpTE->isAltShuffle() &&
3321             OpTE->getOpcode() == Instruction::Store && !Order.empty()) {
3322           SmallVector<int> Mask;
3323           inversePermutation(Order, Mask);
3324           unsigned E = Order.size();
3325           OrdersType CurrentOrder(E, E);
3326           transform(Mask, CurrentOrder.begin(), [E](int Idx) {
3327             return Idx == UndefMaskElem ? E : static_cast<unsigned>(Idx);
3328           });
3329           fixupOrderingIndices(CurrentOrder);
3330           ++OrdersUses.insert(std::make_pair(CurrentOrder, 0)).first->second;
3331         } else {
3332           ++OrdersUses.insert(std::make_pair(Order, 0)).first->second;
3333         }
3334         if (VisitedOps.insert(OpTE).second)
3335           OrdersUses.insert(std::make_pair(OrdersType(), 0)).first->second +=
3336               OpTE->UserTreeIndices.size();
3337         assert(OrdersUses[{}] > 0 && "Counter cannot be less than 0.");
3338         --OrdersUses[{}];
3339       }
3340       // If no orders - skip current nodes and jump to the next one, if any.
3341       if (OrdersUses.empty()) {
3342         for_each(Data.second,
3343                  [&OrderedEntries](const std::pair<unsigned, TreeEntry *> &Op) {
3344                    OrderedEntries.remove(Op.second);
3345                  });
3346         continue;
3347       }
3348       // Choose the best order.
3349       ArrayRef<unsigned> BestOrder = OrdersUses.front().first;
3350       unsigned Cnt = OrdersUses.front().second;
3351       for (const auto &Pair : drop_begin(OrdersUses)) {
3352         if (Cnt < Pair.second || (Cnt == Pair.second && Pair.first.empty())) {
3353           BestOrder = Pair.first;
3354           Cnt = Pair.second;
3355         }
3356       }
3357       // Set order of the user node (reordering of operands and user nodes).
3358       if (BestOrder.empty()) {
3359         for_each(Data.second,
3360                  [&OrderedEntries](const std::pair<unsigned, TreeEntry *> &Op) {
3361                    OrderedEntries.remove(Op.second);
3362                  });
3363         continue;
3364       }
3365       // Erase operands from OrderedEntries list and adjust their orders.
3366       VisitedOps.clear();
3367       SmallVector<int> Mask;
3368       inversePermutation(BestOrder, Mask);
3369       SmallVector<int> MaskOrder(BestOrder.size(), UndefMaskElem);
3370       unsigned E = BestOrder.size();
3371       transform(BestOrder, MaskOrder.begin(), [E](unsigned I) {
3372         return I < E ? static_cast<int>(I) : UndefMaskElem;
3373       });
3374       for (const std::pair<unsigned, TreeEntry *> &Op : Data.second) {
3375         TreeEntry *TE = Op.second;
3376         OrderedEntries.remove(TE);
3377         if (!VisitedOps.insert(TE).second)
3378           continue;
3379         if (!TE->ReuseShuffleIndices.empty() && TE->ReorderIndices.empty()) {
3380           // Just reorder reuses indices.
3381           reorderReuses(TE->ReuseShuffleIndices, Mask);
3382           continue;
3383         }
3384         // Gathers are processed separately.
3385         if (TE->State != TreeEntry::Vectorize)
3386           continue;
3387         assert((BestOrder.size() == TE->ReorderIndices.size() ||
3388                 TE->ReorderIndices.empty()) &&
3389                "Non-matching sizes of user/operand entries.");
3390         reorderOrder(TE->ReorderIndices, Mask);
3391       }
3392       // For gathers just need to reorder its scalars.
3393       for (TreeEntry *Gather : GatherOps) {
3394         assert(Gather->ReorderIndices.empty() &&
3395                "Unexpected reordering of gathers.");
3396         if (!Gather->ReuseShuffleIndices.empty()) {
3397           // Just reorder reuses indices.
3398           reorderReuses(Gather->ReuseShuffleIndices, Mask);
3399           continue;
3400         }
3401         reorderScalars(Gather->Scalars, Mask);
3402         OrderedEntries.remove(Gather);
3403       }
3404       // Reorder operands of the user node and set the ordering for the user
3405       // node itself.
3406       if (Data.first->State != TreeEntry::Vectorize ||
3407           !isa<ExtractElementInst, ExtractValueInst, LoadInst>(
3408               Data.first->getMainOp()) ||
3409           Data.first->isAltShuffle())
3410         Data.first->reorderOperands(Mask);
3411       if (!isa<InsertElementInst, StoreInst>(Data.first->getMainOp()) ||
3412           Data.first->isAltShuffle()) {
3413         reorderScalars(Data.first->Scalars, Mask);
3414         reorderOrder(Data.first->ReorderIndices, MaskOrder);
3415         if (Data.first->ReuseShuffleIndices.empty() &&
3416             !Data.first->ReorderIndices.empty() &&
3417             !Data.first->isAltShuffle()) {
3418           // Insert user node to the list to try to sink reordering deeper in
3419           // the graph.
3420           OrderedEntries.insert(Data.first);
3421         }
3422       } else {
3423         reorderOrder(Data.first->ReorderIndices, Mask);
3424       }
3425     }
3426   }
3427   // If the reordering is unnecessary, just remove the reorder.
3428   if (IgnoreReorder && !VectorizableTree.front()->ReorderIndices.empty() &&
3429       VectorizableTree.front()->ReuseShuffleIndices.empty())
3430     VectorizableTree.front()->ReorderIndices.clear();
3431 }
3432 
3433 void BoUpSLP::buildExternalUses(
3434     const ExtraValueToDebugLocsMap &ExternallyUsedValues) {
3435   // Collect the values that we need to extract from the tree.
3436   for (auto &TEPtr : VectorizableTree) {
3437     TreeEntry *Entry = TEPtr.get();
3438 
3439     // No need to handle users of gathered values.
3440     if (Entry->State == TreeEntry::NeedToGather)
3441       continue;
3442 
3443     // For each lane:
3444     for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
3445       Value *Scalar = Entry->Scalars[Lane];
3446       int FoundLane = Entry->findLaneForValue(Scalar);
3447 
3448       // Check if the scalar is externally used as an extra arg.
3449       auto ExtI = ExternallyUsedValues.find(Scalar);
3450       if (ExtI != ExternallyUsedValues.end()) {
3451         LLVM_DEBUG(dbgs() << "SLP: Need to extract: Extra arg from lane "
3452                           << Lane << " from " << *Scalar << ".\n");
3453         ExternalUses.emplace_back(Scalar, nullptr, FoundLane);
3454       }
3455       for (User *U : Scalar->users()) {
3456         LLVM_DEBUG(dbgs() << "SLP: Checking user:" << *U << ".\n");
3457 
3458         Instruction *UserInst = dyn_cast<Instruction>(U);
3459         if (!UserInst)
3460           continue;
3461 
3462         if (isDeleted(UserInst))
3463           continue;
3464 
3465         // Skip in-tree scalars that become vectors
3466         if (TreeEntry *UseEntry = getTreeEntry(U)) {
3467           Value *UseScalar = UseEntry->Scalars[0];
3468           // Some in-tree scalars will remain as scalar in vectorized
3469           // instructions. If that is the case, the one in Lane 0 will
3470           // be used.
3471           if (UseScalar != U ||
3472               UseEntry->State == TreeEntry::ScatterVectorize ||
3473               !InTreeUserNeedToExtract(Scalar, UserInst, TLI)) {
3474             LLVM_DEBUG(dbgs() << "SLP: \tInternal user will be removed:" << *U
3475                               << ".\n");
3476             assert(UseEntry->State != TreeEntry::NeedToGather && "Bad state");
3477             continue;
3478           }
3479         }
3480 
3481         // Ignore users in the user ignore list.
3482         if (is_contained(UserIgnoreList, UserInst))
3483           continue;
3484 
3485         LLVM_DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane "
3486                           << Lane << " from " << *Scalar << ".\n");
3487         ExternalUses.push_back(ExternalUser(Scalar, U, FoundLane));
3488       }
3489     }
3490   }
3491 }
3492 
3493 void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
3494                         ArrayRef<Value *> UserIgnoreLst) {
3495   deleteTree();
3496   UserIgnoreList = UserIgnoreLst;
3497   if (!allSameType(Roots))
3498     return;
3499   buildTree_rec(Roots, 0, EdgeInfo());
3500 }
3501 
3502 namespace {
3503 /// Tracks the state we can represent the loads in the given sequence.
3504 enum class LoadsState { Gather, Vectorize, ScatterVectorize };
3505 } // anonymous namespace
3506 
3507 /// Checks if the given array of loads can be represented as a vectorized,
3508 /// scatter or just simple gather.
3509 static LoadsState canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
3510                                     const TargetTransformInfo &TTI,
3511                                     const DataLayout &DL, ScalarEvolution &SE,
3512                                     SmallVectorImpl<unsigned> &Order,
3513                                     SmallVectorImpl<Value *> &PointerOps) {
3514   // Check that a vectorized load would load the same memory as a scalar
3515   // load. For example, we don't want to vectorize loads that are smaller
3516   // than 8-bit. Even though we have a packed struct {<i2, i2, i2, i2>} LLVM
3517   // treats loading/storing it as an i8 struct. If we vectorize loads/stores
3518   // from such a struct, we read/write packed bits disagreeing with the
3519   // unvectorized version.
3520   Type *ScalarTy = VL0->getType();
3521 
3522   if (DL.getTypeSizeInBits(ScalarTy) != DL.getTypeAllocSizeInBits(ScalarTy))
3523     return LoadsState::Gather;
3524 
3525   // Make sure all loads in the bundle are simple - we can't vectorize
3526   // atomic or volatile loads.
3527   PointerOps.clear();
3528   PointerOps.resize(VL.size());
3529   auto *POIter = PointerOps.begin();
3530   for (Value *V : VL) {
3531     auto *L = cast<LoadInst>(V);
3532     if (!L->isSimple())
3533       return LoadsState::Gather;
3534     *POIter = L->getPointerOperand();
3535     ++POIter;
3536   }
3537 
3538   Order.clear();
3539   // Check the order of pointer operands.
3540   if (llvm::sortPtrAccesses(PointerOps, ScalarTy, DL, SE, Order)) {
3541     Value *Ptr0;
3542     Value *PtrN;
3543     if (Order.empty()) {
3544       Ptr0 = PointerOps.front();
3545       PtrN = PointerOps.back();
3546     } else {
3547       Ptr0 = PointerOps[Order.front()];
3548       PtrN = PointerOps[Order.back()];
3549     }
3550     Optional<int> Diff =
3551         getPointersDiff(ScalarTy, Ptr0, ScalarTy, PtrN, DL, SE);
3552     // Check that the sorted loads are consecutive.
3553     if (static_cast<unsigned>(*Diff) == VL.size() - 1)
3554       return LoadsState::Vectorize;
3555     Align CommonAlignment = cast<LoadInst>(VL0)->getAlign();
3556     for (Value *V : VL)
3557       CommonAlignment =
3558           commonAlignment(CommonAlignment, cast<LoadInst>(V)->getAlign());
3559     if (TTI.isLegalMaskedGather(FixedVectorType::get(ScalarTy, VL.size()),
3560                                 CommonAlignment))
3561       return LoadsState::ScatterVectorize;
3562   }
3563 
3564   return LoadsState::Gather;
3565 }
3566 
3567 void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
3568                             const EdgeInfo &UserTreeIdx) {
3569   assert((allConstant(VL) || allSameType(VL)) && "Invalid types!");
3570 
3571   SmallVector<int> ReuseShuffleIndicies;
3572   SmallVector<Value *> UniqueValues;
3573   auto &&TryToFindDuplicates = [&VL, &ReuseShuffleIndicies, &UniqueValues,
3574                                 &UserTreeIdx,
3575                                 this](const InstructionsState &S) {
3576     // Check that every instruction appears once in this bundle.
3577     DenseMap<Value *, unsigned> UniquePositions;
3578     for (Value *V : VL) {
3579       if (isConstant(V)) {
3580         ReuseShuffleIndicies.emplace_back(
3581             isa<UndefValue>(V) ? UndefMaskElem : UniqueValues.size());
3582         UniqueValues.emplace_back(V);
3583         continue;
3584       }
3585       auto Res = UniquePositions.try_emplace(V, UniqueValues.size());
3586       ReuseShuffleIndicies.emplace_back(Res.first->second);
3587       if (Res.second)
3588         UniqueValues.emplace_back(V);
3589     }
3590     size_t NumUniqueScalarValues = UniqueValues.size();
3591     if (NumUniqueScalarValues == VL.size()) {
3592       ReuseShuffleIndicies.clear();
3593     } else {
3594       LLVM_DEBUG(dbgs() << "SLP: Shuffle for reused scalars.\n");
3595       if (NumUniqueScalarValues <= 1 ||
3596           (UniquePositions.size() == 1 && all_of(UniqueValues,
3597                                                  [](Value *V) {
3598                                                    return isa<UndefValue>(V) ||
3599                                                           !isConstant(V);
3600                                                  })) ||
3601           !llvm::isPowerOf2_32(NumUniqueScalarValues)) {
3602         LLVM_DEBUG(dbgs() << "SLP: Scalar used twice in bundle.\n");
3603         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3604         return false;
3605       }
3606       VL = UniqueValues;
3607     }
3608     return true;
3609   };
3610 
3611   InstructionsState S = getSameOpcode(VL);
3612   if (Depth == RecursionMaxDepth) {
3613     LLVM_DEBUG(dbgs() << "SLP: Gathering due to max recursion depth.\n");
3614     if (TryToFindDuplicates(S))
3615       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3616                    ReuseShuffleIndicies);
3617     return;
3618   }
3619 
3620   // Don't handle scalable vectors
3621   if (S.getOpcode() == Instruction::ExtractElement &&
3622       isa<ScalableVectorType>(
3623           cast<ExtractElementInst>(S.OpValue)->getVectorOperandType())) {
3624     LLVM_DEBUG(dbgs() << "SLP: Gathering due to scalable vector type.\n");
3625     if (TryToFindDuplicates(S))
3626       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3627                    ReuseShuffleIndicies);
3628     return;
3629   }
3630 
3631   // Don't handle vectors.
3632   if (S.OpValue->getType()->isVectorTy() &&
3633       !isa<InsertElementInst>(S.OpValue)) {
3634     LLVM_DEBUG(dbgs() << "SLP: Gathering due to vector type.\n");
3635     newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3636     return;
3637   }
3638 
3639   if (StoreInst *SI = dyn_cast<StoreInst>(S.OpValue))
3640     if (SI->getValueOperand()->getType()->isVectorTy()) {
3641       LLVM_DEBUG(dbgs() << "SLP: Gathering due to store vector type.\n");
3642       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3643       return;
3644     }
3645 
3646   // If all of the operands are identical or constant we have a simple solution.
3647   // If we deal with insert/extract instructions, they all must have constant
3648   // indices, otherwise we should gather them, not try to vectorize.
3649   if (allConstant(VL) || isSplat(VL) || !allSameBlock(VL) || !S.getOpcode() ||
3650       (isa<InsertElementInst, ExtractValueInst, ExtractElementInst>(S.MainOp) &&
3651        !all_of(VL, isVectorLikeInstWithConstOps))) {
3652     LLVM_DEBUG(dbgs() << "SLP: Gathering due to C,S,B,O. \n");
3653     if (TryToFindDuplicates(S))
3654       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3655                    ReuseShuffleIndicies);
3656     return;
3657   }
3658 
3659   // We now know that this is a vector of instructions of the same type from
3660   // the same block.
3661 
3662   // Don't vectorize ephemeral values.
3663   for (Value *V : VL) {
3664     if (EphValues.count(V)) {
3665       LLVM_DEBUG(dbgs() << "SLP: The instruction (" << *V
3666                         << ") is ephemeral.\n");
3667       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3668       return;
3669     }
3670   }
3671 
3672   // Check if this is a duplicate of another entry.
3673   if (TreeEntry *E = getTreeEntry(S.OpValue)) {
3674     LLVM_DEBUG(dbgs() << "SLP: \tChecking bundle: " << *S.OpValue << ".\n");
3675     if (!E->isSame(VL)) {
3676       LLVM_DEBUG(dbgs() << "SLP: Gathering due to partial overlap.\n");
3677       if (TryToFindDuplicates(S))
3678         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3679                      ReuseShuffleIndicies);
3680       return;
3681     }
3682     // Record the reuse of the tree node.  FIXME, currently this is only used to
3683     // properly draw the graph rather than for the actual vectorization.
3684     E->UserTreeIndices.push_back(UserTreeIdx);
3685     LLVM_DEBUG(dbgs() << "SLP: Perfect diamond merge at " << *S.OpValue
3686                       << ".\n");
3687     return;
3688   }
3689 
3690   // Check that none of the instructions in the bundle are already in the tree.
3691   for (Value *V : VL) {
3692     auto *I = dyn_cast<Instruction>(V);
3693     if (!I)
3694       continue;
3695     if (getTreeEntry(I)) {
3696       LLVM_DEBUG(dbgs() << "SLP: The instruction (" << *V
3697                         << ") is already in tree.\n");
3698       if (TryToFindDuplicates(S))
3699         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3700                      ReuseShuffleIndicies);
3701       return;
3702     }
3703   }
3704 
3705   // The reduction nodes (stored in UserIgnoreList) also should stay scalar.
3706   for (Value *V : VL) {
3707     if (is_contained(UserIgnoreList, V)) {
3708       LLVM_DEBUG(dbgs() << "SLP: Gathering due to gathered scalar.\n");
3709       if (TryToFindDuplicates(S))
3710         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3711                      ReuseShuffleIndicies);
3712       return;
3713     }
3714   }
3715 
3716   // Check that all of the users of the scalars that we want to vectorize are
3717   // schedulable.
3718   auto *VL0 = cast<Instruction>(S.OpValue);
3719   BasicBlock *BB = VL0->getParent();
3720 
3721   if (!DT->isReachableFromEntry(BB)) {
3722     // Don't go into unreachable blocks. They may contain instructions with
3723     // dependency cycles which confuse the final scheduling.
3724     LLVM_DEBUG(dbgs() << "SLP: bundle in unreachable block.\n");
3725     newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3726     return;
3727   }
3728 
3729   // Check that every instruction appears once in this bundle.
3730   if (!TryToFindDuplicates(S))
3731     return;
3732 
3733   auto &BSRef = BlocksSchedules[BB];
3734   if (!BSRef)
3735     BSRef = std::make_unique<BlockScheduling>(BB);
3736 
3737   BlockScheduling &BS = *BSRef.get();
3738 
3739   Optional<ScheduleData *> Bundle = BS.tryScheduleBundle(VL, this, S);
3740   if (!Bundle) {
3741     LLVM_DEBUG(dbgs() << "SLP: We are not able to schedule this bundle!\n");
3742     assert((!BS.getScheduleData(VL0) ||
3743             !BS.getScheduleData(VL0)->isPartOfBundle()) &&
3744            "tryScheduleBundle should cancelScheduling on failure");
3745     newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3746                  ReuseShuffleIndicies);
3747     return;
3748   }
3749   LLVM_DEBUG(dbgs() << "SLP: We are able to schedule this bundle.\n");
3750 
3751   unsigned ShuffleOrOp = S.isAltShuffle() ?
3752                 (unsigned) Instruction::ShuffleVector : S.getOpcode();
3753   switch (ShuffleOrOp) {
3754     case Instruction::PHI: {
3755       auto *PH = cast<PHINode>(VL0);
3756 
3757       // Check for terminator values (e.g. invoke).
3758       for (Value *V : VL)
3759         for (unsigned I = 0, E = PH->getNumIncomingValues(); I < E; ++I) {
3760           Instruction *Term = dyn_cast<Instruction>(
3761               cast<PHINode>(V)->getIncomingValueForBlock(
3762                   PH->getIncomingBlock(I)));
3763           if (Term && Term->isTerminator()) {
3764             LLVM_DEBUG(dbgs()
3765                        << "SLP: Need to swizzle PHINodes (terminator use).\n");
3766             BS.cancelScheduling(VL, VL0);
3767             newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3768                          ReuseShuffleIndicies);
3769             return;
3770           }
3771         }
3772 
3773       TreeEntry *TE =
3774           newTreeEntry(VL, Bundle, S, UserTreeIdx, ReuseShuffleIndicies);
3775       LLVM_DEBUG(dbgs() << "SLP: added a vector of PHINodes.\n");
3776 
3777       // Keeps the reordered operands to avoid code duplication.
3778       SmallVector<ValueList, 2> OperandsVec;
3779       for (unsigned I = 0, E = PH->getNumIncomingValues(); I < E; ++I) {
3780         if (!DT->isReachableFromEntry(PH->getIncomingBlock(I))) {
3781           ValueList Operands(VL.size(), PoisonValue::get(PH->getType()));
3782           TE->setOperand(I, Operands);
3783           OperandsVec.push_back(Operands);
3784           continue;
3785         }
3786         ValueList Operands;
3787         // Prepare the operand vector.
3788         for (Value *V : VL)
3789           Operands.push_back(cast<PHINode>(V)->getIncomingValueForBlock(
3790               PH->getIncomingBlock(I)));
3791         TE->setOperand(I, Operands);
3792         OperandsVec.push_back(Operands);
3793       }
3794       for (unsigned OpIdx = 0, OpE = OperandsVec.size(); OpIdx != OpE; ++OpIdx)
3795         buildTree_rec(OperandsVec[OpIdx], Depth + 1, {TE, OpIdx});
3796       return;
3797     }
3798     case Instruction::ExtractValue:
3799     case Instruction::ExtractElement: {
3800       OrdersType CurrentOrder;
3801       bool Reuse = canReuseExtract(VL, VL0, CurrentOrder);
3802       if (Reuse) {
3803         LLVM_DEBUG(dbgs() << "SLP: Reusing or shuffling extract sequence.\n");
3804         newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3805                      ReuseShuffleIndicies);
3806         // This is a special case, as it does not gather, but at the same time
3807         // we are not extending buildTree_rec() towards the operands.
3808         ValueList Op0;
3809         Op0.assign(VL.size(), VL0->getOperand(0));
3810         VectorizableTree.back()->setOperand(0, Op0);
3811         return;
3812       }
3813       if (!CurrentOrder.empty()) {
3814         LLVM_DEBUG({
3815           dbgs() << "SLP: Reusing or shuffling of reordered extract sequence "
3816                     "with order";
3817           for (unsigned Idx : CurrentOrder)
3818             dbgs() << " " << Idx;
3819           dbgs() << "\n";
3820         });
3821         fixupOrderingIndices(CurrentOrder);
3822         // Insert new order with initial value 0, if it does not exist,
3823         // otherwise return the iterator to the existing one.
3824         newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3825                      ReuseShuffleIndicies, CurrentOrder);
3826         // This is a special case, as it does not gather, but at the same time
3827         // we are not extending buildTree_rec() towards the operands.
3828         ValueList Op0;
3829         Op0.assign(VL.size(), VL0->getOperand(0));
3830         VectorizableTree.back()->setOperand(0, Op0);
3831         return;
3832       }
3833       LLVM_DEBUG(dbgs() << "SLP: Gather extract sequence.\n");
3834       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3835                    ReuseShuffleIndicies);
3836       BS.cancelScheduling(VL, VL0);
3837       return;
3838     }
3839     case Instruction::InsertElement: {
3840       assert(ReuseShuffleIndicies.empty() && "All inserts should be unique");
3841 
3842       // Check that we have a buildvector and not a shuffle of 2 or more
3843       // different vectors.
3844       ValueSet SourceVectors;
3845       int MinIdx = std::numeric_limits<int>::max();
3846       for (Value *V : VL) {
3847         SourceVectors.insert(cast<Instruction>(V)->getOperand(0));
3848         Optional<int> Idx = *getInsertIndex(V, 0);
3849         if (!Idx || *Idx == UndefMaskElem)
3850           continue;
3851         MinIdx = std::min(MinIdx, *Idx);
3852       }
3853 
3854       if (count_if(VL, [&SourceVectors](Value *V) {
3855             return !SourceVectors.contains(V);
3856           }) >= 2) {
3857         // Found 2nd source vector - cancel.
3858         LLVM_DEBUG(dbgs() << "SLP: Gather of insertelement vectors with "
3859                              "different source vectors.\n");
3860         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx);
3861         BS.cancelScheduling(VL, VL0);
3862         return;
3863       }
3864 
3865       auto OrdCompare = [](const std::pair<int, int> &P1,
3866                            const std::pair<int, int> &P2) {
3867         return P1.first > P2.first;
3868       };
3869       PriorityQueue<std::pair<int, int>, SmallVector<std::pair<int, int>>,
3870                     decltype(OrdCompare)>
3871           Indices(OrdCompare);
3872       for (int I = 0, E = VL.size(); I < E; ++I) {
3873         Optional<int> Idx = *getInsertIndex(VL[I], 0);
3874         if (!Idx || *Idx == UndefMaskElem)
3875           continue;
3876         Indices.emplace(*Idx, I);
3877       }
3878       OrdersType CurrentOrder(VL.size(), VL.size());
3879       bool IsIdentity = true;
3880       for (int I = 0, E = VL.size(); I < E; ++I) {
3881         CurrentOrder[Indices.top().second] = I;
3882         IsIdentity &= Indices.top().second == I;
3883         Indices.pop();
3884       }
3885       if (IsIdentity)
3886         CurrentOrder.clear();
3887       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3888                                    None, CurrentOrder);
3889       LLVM_DEBUG(dbgs() << "SLP: added inserts bundle.\n");
3890 
3891       constexpr int NumOps = 2;
3892       ValueList VectorOperands[NumOps];
3893       for (int I = 0; I < NumOps; ++I) {
3894         for (Value *V : VL)
3895           VectorOperands[I].push_back(cast<Instruction>(V)->getOperand(I));
3896 
3897         TE->setOperand(I, VectorOperands[I]);
3898       }
3899       buildTree_rec(VectorOperands[NumOps - 1], Depth + 1, {TE, NumOps - 1});
3900       return;
3901     }
3902     case Instruction::Load: {
3903       // Check that a vectorized load would load the same memory as a scalar
3904       // load. For example, we don't want to vectorize loads that are smaller
3905       // than 8-bit. Even though we have a packed struct {<i2, i2, i2, i2>} LLVM
3906       // treats loading/storing it as an i8 struct. If we vectorize loads/stores
3907       // from such a struct, we read/write packed bits disagreeing with the
3908       // unvectorized version.
3909       SmallVector<Value *> PointerOps;
3910       OrdersType CurrentOrder;
3911       TreeEntry *TE = nullptr;
3912       switch (canVectorizeLoads(VL, VL0, *TTI, *DL, *SE, CurrentOrder,
3913                                 PointerOps)) {
3914       case LoadsState::Vectorize:
3915         if (CurrentOrder.empty()) {
3916           // Original loads are consecutive and does not require reordering.
3917           TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3918                             ReuseShuffleIndicies);
3919           LLVM_DEBUG(dbgs() << "SLP: added a vector of loads.\n");
3920         } else {
3921           fixupOrderingIndices(CurrentOrder);
3922           // Need to reorder.
3923           TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3924                             ReuseShuffleIndicies, CurrentOrder);
3925           LLVM_DEBUG(dbgs() << "SLP: added a vector of jumbled loads.\n");
3926         }
3927         TE->setOperandsInOrder();
3928         break;
3929       case LoadsState::ScatterVectorize:
3930         // Vectorizing non-consecutive loads with `llvm.masked.gather`.
3931         TE = newTreeEntry(VL, TreeEntry::ScatterVectorize, Bundle, S,
3932                           UserTreeIdx, ReuseShuffleIndicies);
3933         TE->setOperandsInOrder();
3934         buildTree_rec(PointerOps, Depth + 1, {TE, 0});
3935         LLVM_DEBUG(dbgs() << "SLP: added a vector of non-consecutive loads.\n");
3936         break;
3937       case LoadsState::Gather:
3938         BS.cancelScheduling(VL, VL0);
3939         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3940                      ReuseShuffleIndicies);
3941 #ifndef NDEBUG
3942         Type *ScalarTy = VL0->getType();
3943         if (DL->getTypeSizeInBits(ScalarTy) !=
3944             DL->getTypeAllocSizeInBits(ScalarTy))
3945           LLVM_DEBUG(dbgs() << "SLP: Gathering loads of non-packed type.\n");
3946         else if (any_of(VL, [](Value *V) {
3947                    return !cast<LoadInst>(V)->isSimple();
3948                  }))
3949           LLVM_DEBUG(dbgs() << "SLP: Gathering non-simple loads.\n");
3950         else
3951           LLVM_DEBUG(dbgs() << "SLP: Gathering non-consecutive loads.\n");
3952 #endif // NDEBUG
3953         break;
3954       }
3955       return;
3956     }
3957     case Instruction::ZExt:
3958     case Instruction::SExt:
3959     case Instruction::FPToUI:
3960     case Instruction::FPToSI:
3961     case Instruction::FPExt:
3962     case Instruction::PtrToInt:
3963     case Instruction::IntToPtr:
3964     case Instruction::SIToFP:
3965     case Instruction::UIToFP:
3966     case Instruction::Trunc:
3967     case Instruction::FPTrunc:
3968     case Instruction::BitCast: {
3969       Type *SrcTy = VL0->getOperand(0)->getType();
3970       for (Value *V : VL) {
3971         Type *Ty = cast<Instruction>(V)->getOperand(0)->getType();
3972         if (Ty != SrcTy || !isValidElementType(Ty)) {
3973           BS.cancelScheduling(VL, VL0);
3974           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
3975                        ReuseShuffleIndicies);
3976           LLVM_DEBUG(dbgs()
3977                      << "SLP: Gathering casts with different src types.\n");
3978           return;
3979         }
3980       }
3981       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
3982                                    ReuseShuffleIndicies);
3983       LLVM_DEBUG(dbgs() << "SLP: added a vector of casts.\n");
3984 
3985       TE->setOperandsInOrder();
3986       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
3987         ValueList Operands;
3988         // Prepare the operand vector.
3989         for (Value *V : VL)
3990           Operands.push_back(cast<Instruction>(V)->getOperand(i));
3991 
3992         buildTree_rec(Operands, Depth + 1, {TE, i});
3993       }
3994       return;
3995     }
3996     case Instruction::ICmp:
3997     case Instruction::FCmp: {
3998       // Check that all of the compares have the same predicate.
3999       CmpInst::Predicate P0 = cast<CmpInst>(VL0)->getPredicate();
4000       CmpInst::Predicate SwapP0 = CmpInst::getSwappedPredicate(P0);
4001       Type *ComparedTy = VL0->getOperand(0)->getType();
4002       for (Value *V : VL) {
4003         CmpInst *Cmp = cast<CmpInst>(V);
4004         if ((Cmp->getPredicate() != P0 && Cmp->getPredicate() != SwapP0) ||
4005             Cmp->getOperand(0)->getType() != ComparedTy) {
4006           BS.cancelScheduling(VL, VL0);
4007           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4008                        ReuseShuffleIndicies);
4009           LLVM_DEBUG(dbgs()
4010                      << "SLP: Gathering cmp with different predicate.\n");
4011           return;
4012         }
4013       }
4014 
4015       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4016                                    ReuseShuffleIndicies);
4017       LLVM_DEBUG(dbgs() << "SLP: added a vector of compares.\n");
4018 
4019       ValueList Left, Right;
4020       if (cast<CmpInst>(VL0)->isCommutative()) {
4021         // Commutative predicate - collect + sort operands of the instructions
4022         // so that each side is more likely to have the same opcode.
4023         assert(P0 == SwapP0 && "Commutative Predicate mismatch");
4024         reorderInputsAccordingToOpcode(VL, Left, Right, *DL, *SE, *this);
4025       } else {
4026         // Collect operands - commute if it uses the swapped predicate.
4027         for (Value *V : VL) {
4028           auto *Cmp = cast<CmpInst>(V);
4029           Value *LHS = Cmp->getOperand(0);
4030           Value *RHS = Cmp->getOperand(1);
4031           if (Cmp->getPredicate() != P0)
4032             std::swap(LHS, RHS);
4033           Left.push_back(LHS);
4034           Right.push_back(RHS);
4035         }
4036       }
4037       TE->setOperand(0, Left);
4038       TE->setOperand(1, Right);
4039       buildTree_rec(Left, Depth + 1, {TE, 0});
4040       buildTree_rec(Right, Depth + 1, {TE, 1});
4041       return;
4042     }
4043     case Instruction::Select:
4044     case Instruction::FNeg:
4045     case Instruction::Add:
4046     case Instruction::FAdd:
4047     case Instruction::Sub:
4048     case Instruction::FSub:
4049     case Instruction::Mul:
4050     case Instruction::FMul:
4051     case Instruction::UDiv:
4052     case Instruction::SDiv:
4053     case Instruction::FDiv:
4054     case Instruction::URem:
4055     case Instruction::SRem:
4056     case Instruction::FRem:
4057     case Instruction::Shl:
4058     case Instruction::LShr:
4059     case Instruction::AShr:
4060     case Instruction::And:
4061     case Instruction::Or:
4062     case Instruction::Xor: {
4063       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4064                                    ReuseShuffleIndicies);
4065       LLVM_DEBUG(dbgs() << "SLP: added a vector of un/bin op.\n");
4066 
4067       // Sort operands of the instructions so that each side is more likely to
4068       // have the same opcode.
4069       if (isa<BinaryOperator>(VL0) && VL0->isCommutative()) {
4070         ValueList Left, Right;
4071         reorderInputsAccordingToOpcode(VL, Left, Right, *DL, *SE, *this);
4072         TE->setOperand(0, Left);
4073         TE->setOperand(1, Right);
4074         buildTree_rec(Left, Depth + 1, {TE, 0});
4075         buildTree_rec(Right, Depth + 1, {TE, 1});
4076         return;
4077       }
4078 
4079       TE->setOperandsInOrder();
4080       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
4081         ValueList Operands;
4082         // Prepare the operand vector.
4083         for (Value *V : VL)
4084           Operands.push_back(cast<Instruction>(V)->getOperand(i));
4085 
4086         buildTree_rec(Operands, Depth + 1, {TE, i});
4087       }
4088       return;
4089     }
4090     case Instruction::GetElementPtr: {
4091       // We don't combine GEPs with complicated (nested) indexing.
4092       for (Value *V : VL) {
4093         if (cast<Instruction>(V)->getNumOperands() != 2) {
4094           LLVM_DEBUG(dbgs() << "SLP: not-vectorizable GEP (nested indexes).\n");
4095           BS.cancelScheduling(VL, VL0);
4096           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4097                        ReuseShuffleIndicies);
4098           return;
4099         }
4100       }
4101 
4102       // We can't combine several GEPs into one vector if they operate on
4103       // different types.
4104       Type *Ty0 = VL0->getOperand(0)->getType();
4105       for (Value *V : VL) {
4106         Type *CurTy = cast<Instruction>(V)->getOperand(0)->getType();
4107         if (Ty0 != CurTy) {
4108           LLVM_DEBUG(dbgs()
4109                      << "SLP: not-vectorizable GEP (different types).\n");
4110           BS.cancelScheduling(VL, VL0);
4111           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4112                        ReuseShuffleIndicies);
4113           return;
4114         }
4115       }
4116 
4117       // We don't combine GEPs with non-constant indexes.
4118       Type *Ty1 = VL0->getOperand(1)->getType();
4119       for (Value *V : VL) {
4120         auto Op = cast<Instruction>(V)->getOperand(1);
4121         if (!isa<ConstantInt>(Op) ||
4122             (Op->getType() != Ty1 &&
4123              Op->getType()->getScalarSizeInBits() >
4124                  DL->getIndexSizeInBits(
4125                      V->getType()->getPointerAddressSpace()))) {
4126           LLVM_DEBUG(dbgs()
4127                      << "SLP: not-vectorizable GEP (non-constant indexes).\n");
4128           BS.cancelScheduling(VL, VL0);
4129           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4130                        ReuseShuffleIndicies);
4131           return;
4132         }
4133       }
4134 
4135       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4136                                    ReuseShuffleIndicies);
4137       LLVM_DEBUG(dbgs() << "SLP: added a vector of GEPs.\n");
4138       SmallVector<ValueList, 2> Operands(2);
4139       // Prepare the operand vector for pointer operands.
4140       for (Value *V : VL)
4141         Operands.front().push_back(
4142             cast<GetElementPtrInst>(V)->getPointerOperand());
4143       TE->setOperand(0, Operands.front());
4144       // Need to cast all indices to the same type before vectorization to
4145       // avoid crash.
4146       // Required to be able to find correct matches between different gather
4147       // nodes and reuse the vectorized values rather than trying to gather them
4148       // again.
4149       int IndexIdx = 1;
4150       Type *VL0Ty = VL0->getOperand(IndexIdx)->getType();
4151       Type *Ty = all_of(VL,
4152                         [VL0Ty, IndexIdx](Value *V) {
4153                           return VL0Ty == cast<GetElementPtrInst>(V)
4154                                               ->getOperand(IndexIdx)
4155                                               ->getType();
4156                         })
4157                      ? VL0Ty
4158                      : DL->getIndexType(cast<GetElementPtrInst>(VL0)
4159                                             ->getPointerOperandType()
4160                                             ->getScalarType());
4161       // Prepare the operand vector.
4162       for (Value *V : VL) {
4163         auto *Op = cast<Instruction>(V)->getOperand(IndexIdx);
4164         auto *CI = cast<ConstantInt>(Op);
4165         Operands.back().push_back(ConstantExpr::getIntegerCast(
4166             CI, Ty, CI->getValue().isSignBitSet()));
4167       }
4168       TE->setOperand(IndexIdx, Operands.back());
4169 
4170       for (unsigned I = 0, Ops = Operands.size(); I < Ops; ++I)
4171         buildTree_rec(Operands[I], Depth + 1, {TE, I});
4172       return;
4173     }
4174     case Instruction::Store: {
4175       // Check if the stores are consecutive or if we need to swizzle them.
4176       llvm::Type *ScalarTy = cast<StoreInst>(VL0)->getValueOperand()->getType();
4177       // Avoid types that are padded when being allocated as scalars, while
4178       // being packed together in a vector (such as i1).
4179       if (DL->getTypeSizeInBits(ScalarTy) !=
4180           DL->getTypeAllocSizeInBits(ScalarTy)) {
4181         BS.cancelScheduling(VL, VL0);
4182         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4183                      ReuseShuffleIndicies);
4184         LLVM_DEBUG(dbgs() << "SLP: Gathering stores of non-packed type.\n");
4185         return;
4186       }
4187       // Make sure all stores in the bundle are simple - we can't vectorize
4188       // atomic or volatile stores.
4189       SmallVector<Value *, 4> PointerOps(VL.size());
4190       ValueList Operands(VL.size());
4191       auto POIter = PointerOps.begin();
4192       auto OIter = Operands.begin();
4193       for (Value *V : VL) {
4194         auto *SI = cast<StoreInst>(V);
4195         if (!SI->isSimple()) {
4196           BS.cancelScheduling(VL, VL0);
4197           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4198                        ReuseShuffleIndicies);
4199           LLVM_DEBUG(dbgs() << "SLP: Gathering non-simple stores.\n");
4200           return;
4201         }
4202         *POIter = SI->getPointerOperand();
4203         *OIter = SI->getValueOperand();
4204         ++POIter;
4205         ++OIter;
4206       }
4207 
4208       OrdersType CurrentOrder;
4209       // Check the order of pointer operands.
4210       if (llvm::sortPtrAccesses(PointerOps, ScalarTy, *DL, *SE, CurrentOrder)) {
4211         Value *Ptr0;
4212         Value *PtrN;
4213         if (CurrentOrder.empty()) {
4214           Ptr0 = PointerOps.front();
4215           PtrN = PointerOps.back();
4216         } else {
4217           Ptr0 = PointerOps[CurrentOrder.front()];
4218           PtrN = PointerOps[CurrentOrder.back()];
4219         }
4220         Optional<int> Dist =
4221             getPointersDiff(ScalarTy, Ptr0, ScalarTy, PtrN, *DL, *SE);
4222         // Check that the sorted pointer operands are consecutive.
4223         if (static_cast<unsigned>(*Dist) == VL.size() - 1) {
4224           if (CurrentOrder.empty()) {
4225             // Original stores are consecutive and does not require reordering.
4226             TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S,
4227                                          UserTreeIdx, ReuseShuffleIndicies);
4228             TE->setOperandsInOrder();
4229             buildTree_rec(Operands, Depth + 1, {TE, 0});
4230             LLVM_DEBUG(dbgs() << "SLP: added a vector of stores.\n");
4231           } else {
4232             fixupOrderingIndices(CurrentOrder);
4233             TreeEntry *TE =
4234                 newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4235                              ReuseShuffleIndicies, CurrentOrder);
4236             TE->setOperandsInOrder();
4237             buildTree_rec(Operands, Depth + 1, {TE, 0});
4238             LLVM_DEBUG(dbgs() << "SLP: added a vector of jumbled stores.\n");
4239           }
4240           return;
4241         }
4242       }
4243 
4244       BS.cancelScheduling(VL, VL0);
4245       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4246                    ReuseShuffleIndicies);
4247       LLVM_DEBUG(dbgs() << "SLP: Non-consecutive store.\n");
4248       return;
4249     }
4250     case Instruction::Call: {
4251       // Check if the calls are all to the same vectorizable intrinsic or
4252       // library function.
4253       CallInst *CI = cast<CallInst>(VL0);
4254       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
4255 
4256       VFShape Shape = VFShape::get(
4257           *CI, ElementCount::getFixed(static_cast<unsigned int>(VL.size())),
4258           false /*HasGlobalPred*/);
4259       Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);
4260 
4261       if (!VecFunc && !isTriviallyVectorizable(ID)) {
4262         BS.cancelScheduling(VL, VL0);
4263         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4264                      ReuseShuffleIndicies);
4265         LLVM_DEBUG(dbgs() << "SLP: Non-vectorizable call.\n");
4266         return;
4267       }
4268       Function *F = CI->getCalledFunction();
4269       unsigned NumArgs = CI->arg_size();
4270       SmallVector<Value*, 4> ScalarArgs(NumArgs, nullptr);
4271       for (unsigned j = 0; j != NumArgs; ++j)
4272         if (hasVectorInstrinsicScalarOpd(ID, j))
4273           ScalarArgs[j] = CI->getArgOperand(j);
4274       for (Value *V : VL) {
4275         CallInst *CI2 = dyn_cast<CallInst>(V);
4276         if (!CI2 || CI2->getCalledFunction() != F ||
4277             getVectorIntrinsicIDForCall(CI2, TLI) != ID ||
4278             (VecFunc &&
4279              VecFunc != VFDatabase(*CI2).getVectorizedFunction(Shape)) ||
4280             !CI->hasIdenticalOperandBundleSchema(*CI2)) {
4281           BS.cancelScheduling(VL, VL0);
4282           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4283                        ReuseShuffleIndicies);
4284           LLVM_DEBUG(dbgs() << "SLP: mismatched calls:" << *CI << "!=" << *V
4285                             << "\n");
4286           return;
4287         }
4288         // Some intrinsics have scalar arguments and should be same in order for
4289         // them to be vectorized.
4290         for (unsigned j = 0; j != NumArgs; ++j) {
4291           if (hasVectorInstrinsicScalarOpd(ID, j)) {
4292             Value *A1J = CI2->getArgOperand(j);
4293             if (ScalarArgs[j] != A1J) {
4294               BS.cancelScheduling(VL, VL0);
4295               newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4296                            ReuseShuffleIndicies);
4297               LLVM_DEBUG(dbgs() << "SLP: mismatched arguments in call:" << *CI
4298                                 << " argument " << ScalarArgs[j] << "!=" << A1J
4299                                 << "\n");
4300               return;
4301             }
4302           }
4303         }
4304         // Verify that the bundle operands are identical between the two calls.
4305         if (CI->hasOperandBundles() &&
4306             !std::equal(CI->op_begin() + CI->getBundleOperandsStartIndex(),
4307                         CI->op_begin() + CI->getBundleOperandsEndIndex(),
4308                         CI2->op_begin() + CI2->getBundleOperandsStartIndex())) {
4309           BS.cancelScheduling(VL, VL0);
4310           newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4311                        ReuseShuffleIndicies);
4312           LLVM_DEBUG(dbgs() << "SLP: mismatched bundle operands in calls:"
4313                             << *CI << "!=" << *V << '\n');
4314           return;
4315         }
4316       }
4317 
4318       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4319                                    ReuseShuffleIndicies);
4320       TE->setOperandsInOrder();
4321       for (unsigned i = 0, e = CI->arg_size(); i != e; ++i) {
4322         // For scalar operands no need to to create an entry since no need to
4323         // vectorize it.
4324         if (hasVectorInstrinsicScalarOpd(ID, i))
4325           continue;
4326         ValueList Operands;
4327         // Prepare the operand vector.
4328         for (Value *V : VL) {
4329           auto *CI2 = cast<CallInst>(V);
4330           Operands.push_back(CI2->getArgOperand(i));
4331         }
4332         buildTree_rec(Operands, Depth + 1, {TE, i});
4333       }
4334       return;
4335     }
4336     case Instruction::ShuffleVector: {
4337       // If this is not an alternate sequence of opcode like add-sub
4338       // then do not vectorize this instruction.
4339       if (!S.isAltShuffle()) {
4340         BS.cancelScheduling(VL, VL0);
4341         newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4342                      ReuseShuffleIndicies);
4343         LLVM_DEBUG(dbgs() << "SLP: ShuffleVector are not vectorized.\n");
4344         return;
4345       }
4346       TreeEntry *TE = newTreeEntry(VL, Bundle /*vectorized*/, S, UserTreeIdx,
4347                                    ReuseShuffleIndicies);
4348       LLVM_DEBUG(dbgs() << "SLP: added a ShuffleVector op.\n");
4349 
4350       // Reorder operands if reordering would enable vectorization.
4351       if (isa<BinaryOperator>(VL0)) {
4352         ValueList Left, Right;
4353         reorderInputsAccordingToOpcode(VL, Left, Right, *DL, *SE, *this);
4354         TE->setOperand(0, Left);
4355         TE->setOperand(1, Right);
4356         buildTree_rec(Left, Depth + 1, {TE, 0});
4357         buildTree_rec(Right, Depth + 1, {TE, 1});
4358         return;
4359       }
4360 
4361       TE->setOperandsInOrder();
4362       for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
4363         ValueList Operands;
4364         // Prepare the operand vector.
4365         for (Value *V : VL)
4366           Operands.push_back(cast<Instruction>(V)->getOperand(i));
4367 
4368         buildTree_rec(Operands, Depth + 1, {TE, i});
4369       }
4370       return;
4371     }
4372     default:
4373       BS.cancelScheduling(VL, VL0);
4374       newTreeEntry(VL, None /*not vectorized*/, S, UserTreeIdx,
4375                    ReuseShuffleIndicies);
4376       LLVM_DEBUG(dbgs() << "SLP: Gathering unknown instruction.\n");
4377       return;
4378   }
4379 }
4380 
4381 unsigned BoUpSLP::canMapToVector(Type *T, const DataLayout &DL) const {
4382   unsigned N = 1;
4383   Type *EltTy = T;
4384 
4385   while (isa<StructType>(EltTy) || isa<ArrayType>(EltTy) ||
4386          isa<VectorType>(EltTy)) {
4387     if (auto *ST = dyn_cast<StructType>(EltTy)) {
4388       // Check that struct is homogeneous.
4389       for (const auto *Ty : ST->elements())
4390         if (Ty != *ST->element_begin())
4391           return 0;
4392       N *= ST->getNumElements();
4393       EltTy = *ST->element_begin();
4394     } else if (auto *AT = dyn_cast<ArrayType>(EltTy)) {
4395       N *= AT->getNumElements();
4396       EltTy = AT->getElementType();
4397     } else {
4398       auto *VT = cast<FixedVectorType>(EltTy);
4399       N *= VT->getNumElements();
4400       EltTy = VT->getElementType();
4401     }
4402   }
4403 
4404   if (!isValidElementType(EltTy))
4405     return 0;
4406   uint64_t VTSize = DL.getTypeStoreSizeInBits(FixedVectorType::get(EltTy, N));
4407   if (VTSize < MinVecRegSize || VTSize > MaxVecRegSize || VTSize != DL.getTypeStoreSizeInBits(T))
4408     return 0;
4409   return N;
4410 }
4411 
4412 bool BoUpSLP::canReuseExtract(ArrayRef<Value *> VL, Value *OpValue,
4413                               SmallVectorImpl<unsigned> &CurrentOrder) const {
4414   const auto *It = find_if(VL, [](Value *V) {
4415     return isa<ExtractElementInst, ExtractValueInst>(V);
4416   });
4417   assert(It != VL.end() && "Expected at least one extract instruction.");
4418   auto *E0 = cast<Instruction>(*It);
4419   assert(all_of(VL,
4420                 [](Value *V) {
4421                   return isa<UndefValue, ExtractElementInst, ExtractValueInst>(
4422                       V);
4423                 }) &&
4424          "Invalid opcode");
4425   // Check if all of the extracts come from the same vector and from the
4426   // correct offset.
4427   Value *Vec = E0->getOperand(0);
4428 
4429   CurrentOrder.clear();
4430 
4431   // We have to extract from a vector/aggregate with the same number of elements.
4432   unsigned NElts;
4433   if (E0->getOpcode() == Instruction::ExtractValue) {
4434     const DataLayout &DL = E0->getModule()->getDataLayout();
4435     NElts = canMapToVector(Vec->getType(), DL);
4436     if (!NElts)
4437       return false;
4438     // Check if load can be rewritten as load of vector.
4439     LoadInst *LI = dyn_cast<LoadInst>(Vec);
4440     if (!LI || !LI->isSimple() || !LI->hasNUses(VL.size()))
4441       return false;
4442   } else {
4443     NElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
4444   }
4445 
4446   if (NElts != VL.size())
4447     return false;
4448 
4449   // Check that all of the indices extract from the correct offset.
4450   bool ShouldKeepOrder = true;
4451   unsigned E = VL.size();
4452   // Assign to all items the initial value E + 1 so we can check if the extract
4453   // instruction index was used already.
4454   // Also, later we can check that all the indices are used and we have a
4455   // consecutive access in the extract instructions, by checking that no
4456   // element of CurrentOrder still has value E + 1.
4457   CurrentOrder.assign(E, E);
4458   unsigned I = 0;
4459   for (; I < E; ++I) {
4460     auto *Inst = dyn_cast<Instruction>(VL[I]);
4461     if (!Inst)
4462       continue;
4463     if (Inst->getOperand(0) != Vec)
4464       break;
4465     if (auto *EE = dyn_cast<ExtractElementInst>(Inst))
4466       if (isa<UndefValue>(EE->getIndexOperand()))
4467         continue;
4468     Optional<unsigned> Idx = getExtractIndex(Inst);
4469     if (!Idx)
4470       break;
4471     const unsigned ExtIdx = *Idx;
4472     if (ExtIdx != I) {
4473       if (ExtIdx >= E || CurrentOrder[ExtIdx] != E)
4474         break;
4475       ShouldKeepOrder = false;
4476       CurrentOrder[ExtIdx] = I;
4477     } else {
4478       if (CurrentOrder[I] != E)
4479         break;
4480       CurrentOrder[I] = I;
4481     }
4482   }
4483   if (I < E) {
4484     CurrentOrder.clear();
4485     return false;
4486   }
4487   if (ShouldKeepOrder)
4488     CurrentOrder.clear();
4489 
4490   return ShouldKeepOrder;
4491 }
4492 
4493 bool BoUpSLP::areAllUsersVectorized(Instruction *I,
4494                                     ArrayRef<Value *> VectorizedVals) const {
4495   return (I->hasOneUse() && is_contained(VectorizedVals, I)) ||
4496          all_of(I->users(), [this](User *U) {
4497            return ScalarToTreeEntry.count(U) > 0 || MustGather.contains(U);
4498          });
4499 }
4500 
4501 static std::pair<InstructionCost, InstructionCost>
4502 getVectorCallCosts(CallInst *CI, FixedVectorType *VecTy,
4503                    TargetTransformInfo *TTI, TargetLibraryInfo *TLI) {
4504   Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
4505 
4506   // Calculate the cost of the scalar and vector calls.
4507   SmallVector<Type *, 4> VecTys;
4508   for (Use &Arg : CI->args())
4509     VecTys.push_back(
4510         FixedVectorType::get(Arg->getType(), VecTy->getNumElements()));
4511   FastMathFlags FMF;
4512   if (auto *FPCI = dyn_cast<FPMathOperator>(CI))
4513     FMF = FPCI->getFastMathFlags();
4514   SmallVector<const Value *> Arguments(CI->args());
4515   IntrinsicCostAttributes CostAttrs(ID, VecTy, Arguments, VecTys, FMF,
4516                                     dyn_cast<IntrinsicInst>(CI));
4517   auto IntrinsicCost =
4518     TTI->getIntrinsicInstrCost(CostAttrs, TTI::TCK_RecipThroughput);
4519 
4520   auto Shape = VFShape::get(*CI, ElementCount::getFixed(static_cast<unsigned>(
4521                                      VecTy->getNumElements())),
4522                             false /*HasGlobalPred*/);
4523   Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);
4524   auto LibCost = IntrinsicCost;
4525   if (!CI->isNoBuiltin() && VecFunc) {
4526     // Calculate the cost of the vector library call.
4527     // If the corresponding vector call is cheaper, return its cost.
4528     LibCost = TTI->getCallInstrCost(nullptr, VecTy, VecTys,
4529                                     TTI::TCK_RecipThroughput);
4530   }
4531   return {IntrinsicCost, LibCost};
4532 }
4533 
4534 /// Compute the cost of creating a vector of type \p VecTy containing the
4535 /// extracted values from \p VL.
4536 static InstructionCost
4537 computeExtractCost(ArrayRef<Value *> VL, FixedVectorType *VecTy,
4538                    TargetTransformInfo::ShuffleKind ShuffleKind,
4539                    ArrayRef<int> Mask, TargetTransformInfo &TTI) {
4540   unsigned NumOfParts = TTI.getNumberOfParts(VecTy);
4541 
4542   if (ShuffleKind != TargetTransformInfo::SK_PermuteSingleSrc || !NumOfParts ||
4543       VecTy->getNumElements() < NumOfParts)
4544     return TTI.getShuffleCost(ShuffleKind, VecTy, Mask);
4545 
4546   bool AllConsecutive = true;
4547   unsigned EltsPerVector = VecTy->getNumElements() / NumOfParts;
4548   unsigned Idx = -1;
4549   InstructionCost Cost = 0;
4550 
4551   // Process extracts in blocks of EltsPerVector to check if the source vector
4552   // operand can be re-used directly. If not, add the cost of creating a shuffle
4553   // to extract the values into a vector register.
4554   for (auto *V : VL) {
4555     ++Idx;
4556 
4557     // Need to exclude undefs from analysis.
4558     if (isa<UndefValue>(V) || Mask[Idx] == UndefMaskElem)
4559       continue;
4560 
4561     // Reached the start of a new vector registers.
4562     if (Idx % EltsPerVector == 0) {
4563       AllConsecutive = true;
4564       continue;
4565     }
4566 
4567     // Check all extracts for a vector register on the target directly
4568     // extract values in order.
4569     unsigned CurrentIdx = *getExtractIndex(cast<Instruction>(V));
4570     if (!isa<UndefValue>(VL[Idx - 1]) && Mask[Idx - 1] != UndefMaskElem) {
4571       unsigned PrevIdx = *getExtractIndex(cast<Instruction>(VL[Idx - 1]));
4572       AllConsecutive &= PrevIdx + 1 == CurrentIdx &&
4573                         CurrentIdx % EltsPerVector == Idx % EltsPerVector;
4574     }
4575 
4576     if (AllConsecutive)
4577       continue;
4578 
4579     // Skip all indices, except for the last index per vector block.
4580     if ((Idx + 1) % EltsPerVector != 0 && Idx + 1 != VL.size())
4581       continue;
4582 
4583     // If we have a series of extracts which are not consecutive and hence
4584     // cannot re-use the source vector register directly, compute the shuffle
4585     // cost to extract the a vector with EltsPerVector elements.
4586     Cost += TTI.getShuffleCost(
4587         TargetTransformInfo::SK_PermuteSingleSrc,
4588         FixedVectorType::get(VecTy->getElementType(), EltsPerVector));
4589   }
4590   return Cost;
4591 }
4592 
4593 /// Build shuffle mask for shuffle graph entries and lists of main and alternate
4594 /// operations operands.
4595 static void
4596 buildSuffleEntryMask(ArrayRef<Value *> VL, ArrayRef<unsigned> ReorderIndices,
4597                      ArrayRef<int> ReusesIndices,
4598                      const function_ref<bool(Instruction *)> IsAltOp,
4599                      SmallVectorImpl<int> &Mask,
4600                      SmallVectorImpl<Value *> *OpScalars = nullptr,
4601                      SmallVectorImpl<Value *> *AltScalars = nullptr) {
4602   unsigned Sz = VL.size();
4603   Mask.assign(Sz, UndefMaskElem);
4604   SmallVector<int> OrderMask;
4605   if (!ReorderIndices.empty())
4606     inversePermutation(ReorderIndices, OrderMask);
4607   for (unsigned I = 0; I < Sz; ++I) {
4608     unsigned Idx = I;
4609     if (!ReorderIndices.empty())
4610       Idx = OrderMask[I];
4611     auto *OpInst = cast<Instruction>(VL[Idx]);
4612     if (IsAltOp(OpInst)) {
4613       Mask[I] = Sz + Idx;
4614       if (AltScalars)
4615         AltScalars->push_back(OpInst);
4616     } else {
4617       Mask[I] = Idx;
4618       if (OpScalars)
4619         OpScalars->push_back(OpInst);
4620     }
4621   }
4622   if (!ReusesIndices.empty()) {
4623     SmallVector<int> NewMask(ReusesIndices.size(), UndefMaskElem);
4624     transform(ReusesIndices, NewMask.begin(), [&Mask](int Idx) {
4625       return Idx != UndefMaskElem ? Mask[Idx] : UndefMaskElem;
4626     });
4627     Mask.swap(NewMask);
4628   }
4629 }
4630 
4631 InstructionCost BoUpSLP::getEntryCost(const TreeEntry *E,
4632                                       ArrayRef<Value *> VectorizedVals) {
4633   ArrayRef<Value*> VL = E->Scalars;
4634 
4635   Type *ScalarTy = VL[0]->getType();
4636   if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
4637     ScalarTy = SI->getValueOperand()->getType();
4638   else if (CmpInst *CI = dyn_cast<CmpInst>(VL[0]))
4639     ScalarTy = CI->getOperand(0)->getType();
4640   else if (auto *IE = dyn_cast<InsertElementInst>(VL[0]))
4641     ScalarTy = IE->getOperand(1)->getType();
4642   auto *VecTy = FixedVectorType::get(ScalarTy, VL.size());
4643   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
4644 
4645   // If we have computed a smaller type for the expression, update VecTy so
4646   // that the costs will be accurate.
4647   if (MinBWs.count(VL[0]))
4648     VecTy = FixedVectorType::get(
4649         IntegerType::get(F->getContext(), MinBWs[VL[0]].first), VL.size());
4650   unsigned EntryVF = E->getVectorFactor();
4651   auto *FinalVecTy = FixedVectorType::get(VecTy->getElementType(), EntryVF);
4652 
4653   bool NeedToShuffleReuses = !E->ReuseShuffleIndices.empty();
4654   // FIXME: it tries to fix a problem with MSVC buildbots.
4655   TargetTransformInfo &TTIRef = *TTI;
4656   auto &&AdjustExtractsCost = [this, &TTIRef, CostKind, VL, VecTy,
4657                                VectorizedVals, E](InstructionCost &Cost) {
4658     DenseMap<Value *, int> ExtractVectorsTys;
4659     SmallPtrSet<Value *, 4> CheckedExtracts;
4660     for (auto *V : VL) {
4661       if (isa<UndefValue>(V))
4662         continue;
4663       // If all users of instruction are going to be vectorized and this
4664       // instruction itself is not going to be vectorized, consider this
4665       // instruction as dead and remove its cost from the final cost of the
4666       // vectorized tree.
4667       // Also, avoid adjusting the cost for extractelements with multiple uses
4668       // in different graph entries.
4669       const TreeEntry *VE = getTreeEntry(V);
4670       if (!CheckedExtracts.insert(V).second ||
4671           !areAllUsersVectorized(cast<Instruction>(V), VectorizedVals) ||
4672           (VE && VE != E))
4673         continue;
4674       auto *EE = cast<ExtractElementInst>(V);
4675       Optional<unsigned> EEIdx = getExtractIndex(EE);
4676       if (!EEIdx)
4677         continue;
4678       unsigned Idx = *EEIdx;
4679       if (TTIRef.getNumberOfParts(VecTy) !=
4680           TTIRef.getNumberOfParts(EE->getVectorOperandType())) {
4681         auto It =
4682             ExtractVectorsTys.try_emplace(EE->getVectorOperand(), Idx).first;
4683         It->getSecond() = std::min<int>(It->second, Idx);
4684       }
4685       // Take credit for instruction that will become dead.
4686       if (EE->hasOneUse()) {
4687         Instruction *Ext = EE->user_back();
4688         if ((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
4689             all_of(Ext->users(),
4690                    [](User *U) { return isa<GetElementPtrInst>(U); })) {
4691           // Use getExtractWithExtendCost() to calculate the cost of
4692           // extractelement/ext pair.
4693           Cost -=
4694               TTIRef.getExtractWithExtendCost(Ext->getOpcode(), Ext->getType(),
4695                                               EE->getVectorOperandType(), Idx);
4696           // Add back the cost of s|zext which is subtracted separately.
4697           Cost += TTIRef.getCastInstrCost(
4698               Ext->getOpcode(), Ext->getType(), EE->getType(),
4699               TTI::getCastContextHint(Ext), CostKind, Ext);
4700           continue;
4701         }
4702       }
4703       Cost -= TTIRef.getVectorInstrCost(Instruction::ExtractElement,
4704                                         EE->getVectorOperandType(), Idx);
4705     }
4706     // Add a cost for subvector extracts/inserts if required.
4707     for (const auto &Data : ExtractVectorsTys) {
4708       auto *EEVTy = cast<FixedVectorType>(Data.first->getType());
4709       unsigned NumElts = VecTy->getNumElements();
4710       if (Data.second % NumElts == 0)
4711         continue;
4712       if (TTIRef.getNumberOfParts(EEVTy) > TTIRef.getNumberOfParts(VecTy)) {
4713         unsigned Idx = (Data.second / NumElts) * NumElts;
4714         unsigned EENumElts = EEVTy->getNumElements();
4715         if (Idx + NumElts <= EENumElts) {
4716           Cost +=
4717               TTIRef.getShuffleCost(TargetTransformInfo::SK_ExtractSubvector,
4718                                     EEVTy, None, Idx, VecTy);
4719         } else {
4720           // Need to round up the subvector type vectorization factor to avoid a
4721           // crash in cost model functions. Make SubVT so that Idx + VF of SubVT
4722           // <= EENumElts.
4723           auto *SubVT =
4724               FixedVectorType::get(VecTy->getElementType(), EENumElts - Idx);
4725           Cost +=
4726               TTIRef.getShuffleCost(TargetTransformInfo::SK_ExtractSubvector,
4727                                     EEVTy, None, Idx, SubVT);
4728         }
4729       } else {
4730         Cost += TTIRef.getShuffleCost(TargetTransformInfo::SK_InsertSubvector,
4731                                       VecTy, None, 0, EEVTy);
4732       }
4733     }
4734   };
4735   if (E->State == TreeEntry::NeedToGather) {
4736     if (allConstant(VL))
4737       return 0;
4738     if (isa<InsertElementInst>(VL[0]))
4739       return InstructionCost::getInvalid();
4740     SmallVector<int> Mask;
4741     SmallVector<const TreeEntry *> Entries;
4742     Optional<TargetTransformInfo::ShuffleKind> Shuffle =
4743         isGatherShuffledEntry(E, Mask, Entries);
4744     if (Shuffle.hasValue()) {
4745       InstructionCost GatherCost = 0;
4746       if (ShuffleVectorInst::isIdentityMask(Mask)) {
4747         // Perfect match in the graph, will reuse the previously vectorized
4748         // node. Cost is 0.
4749         LLVM_DEBUG(
4750             dbgs()
4751             << "SLP: perfect diamond match for gather bundle that starts with "
4752             << *VL.front() << ".\n");
4753         if (NeedToShuffleReuses)
4754           GatherCost =
4755               TTI->getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc,
4756                                   FinalVecTy, E->ReuseShuffleIndices);
4757       } else {
4758         LLVM_DEBUG(dbgs() << "SLP: shuffled " << Entries.size()
4759                           << " entries for bundle that starts with "
4760                           << *VL.front() << ".\n");
4761         // Detected that instead of gather we can emit a shuffle of single/two
4762         // previously vectorized nodes. Add the cost of the permutation rather
4763         // than gather.
4764         ::addMask(Mask, E->ReuseShuffleIndices);
4765         GatherCost = TTI->getShuffleCost(*Shuffle, FinalVecTy, Mask);
4766       }
4767       return GatherCost;
4768     }
4769     if ((E->getOpcode() == Instruction::ExtractElement ||
4770          all_of(E->Scalars,
4771                 [](Value *V) {
4772                   return isa<ExtractElementInst, UndefValue>(V);
4773                 })) &&
4774         allSameType(VL)) {
4775       // Check that gather of extractelements can be represented as just a
4776       // shuffle of a single/two vectors the scalars are extracted from.
4777       SmallVector<int> Mask;
4778       Optional<TargetTransformInfo::ShuffleKind> ShuffleKind =
4779           isFixedVectorShuffle(VL, Mask);
4780       if (ShuffleKind.hasValue()) {
4781         // Found the bunch of extractelement instructions that must be gathered
4782         // into a vector and can be represented as a permutation elements in a
4783         // single input vector or of 2 input vectors.
4784         InstructionCost Cost =
4785             computeExtractCost(VL, VecTy, *ShuffleKind, Mask, *TTI);
4786         AdjustExtractsCost(Cost);
4787         if (NeedToShuffleReuses)
4788           Cost += TTI->getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc,
4789                                       FinalVecTy, E->ReuseShuffleIndices);
4790         return Cost;
4791       }
4792     }
4793     if (isSplat(VL)) {
4794       // Found the broadcasting of the single scalar, calculate the cost as the
4795       // broadcast.
4796       assert(VecTy == FinalVecTy &&
4797              "No reused scalars expected for broadcast.");
4798       return TTI->getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy);
4799     }
4800     InstructionCost ReuseShuffleCost = 0;
4801     if (NeedToShuffleReuses)
4802       ReuseShuffleCost = TTI->getShuffleCost(
4803           TTI::SK_PermuteSingleSrc, FinalVecTy, E->ReuseShuffleIndices);
4804     // Improve gather cost for gather of loads, if we can group some of the
4805     // loads into vector loads.
4806     if (VL.size() > 2 && E->getOpcode() == Instruction::Load &&
4807         !E->isAltShuffle()) {
4808       BoUpSLP::ValueSet VectorizedLoads;
4809       unsigned StartIdx = 0;
4810       unsigned VF = VL.size() / 2;
4811       unsigned VectorizedCnt = 0;
4812       unsigned ScatterVectorizeCnt = 0;
4813       const unsigned Sz = DL->getTypeSizeInBits(E->getMainOp()->getType());
4814       for (unsigned MinVF = getMinVF(2 * Sz); VF >= MinVF; VF /= 2) {
4815         for (unsigned Cnt = StartIdx, End = VL.size(); Cnt + VF <= End;
4816              Cnt += VF) {
4817           ArrayRef<Value *> Slice = VL.slice(Cnt, VF);
4818           if (!VectorizedLoads.count(Slice.front()) &&
4819               !VectorizedLoads.count(Slice.back()) && allSameBlock(Slice)) {
4820             SmallVector<Value *> PointerOps;
4821             OrdersType CurrentOrder;
4822             LoadsState LS = canVectorizeLoads(Slice, Slice.front(), *TTI, *DL,
4823                                               *SE, CurrentOrder, PointerOps);
4824             switch (LS) {
4825             case LoadsState::Vectorize:
4826             case LoadsState::ScatterVectorize:
4827               // Mark the vectorized loads so that we don't vectorize them
4828               // again.
4829               if (LS == LoadsState::Vectorize)
4830                 ++VectorizedCnt;
4831               else
4832                 ++ScatterVectorizeCnt;
4833               VectorizedLoads.insert(Slice.begin(), Slice.end());
4834               // If we vectorized initial block, no need to try to vectorize it
4835               // again.
4836               if (Cnt == StartIdx)
4837                 StartIdx += VF;
4838               break;
4839             case LoadsState::Gather:
4840               break;
4841             }
4842           }
4843         }
4844         // Check if the whole array was vectorized already - exit.
4845         if (StartIdx >= VL.size())
4846           break;
4847         // Found vectorizable parts - exit.
4848         if (!VectorizedLoads.empty())
4849           break;
4850       }
4851       if (!VectorizedLoads.empty()) {
4852         InstructionCost GatherCost = 0;
4853         unsigned NumParts = TTI->getNumberOfParts(VecTy);
4854         bool NeedInsertSubvectorAnalysis =
4855             !NumParts || (VL.size() / VF) > NumParts;
4856         // Get the cost for gathered loads.
4857         for (unsigned I = 0, End = VL.size(); I < End; I += VF) {
4858           if (VectorizedLoads.contains(VL[I]))
4859             continue;
4860           GatherCost += getGatherCost(VL.slice(I, VF));
4861         }
4862         // The cost for vectorized loads.
4863         InstructionCost ScalarsCost = 0;
4864         for (Value *V : VectorizedLoads) {
4865           auto *LI = cast<LoadInst>(V);
4866           ScalarsCost += TTI->getMemoryOpCost(
4867               Instruction::Load, LI->getType(), LI->getAlign(),
4868               LI->getPointerAddressSpace(), CostKind, LI);
4869         }
4870         auto *LI = cast<LoadInst>(E->getMainOp());
4871         auto *LoadTy = FixedVectorType::get(LI->getType(), VF);
4872         Align Alignment = LI->getAlign();
4873         GatherCost +=
4874             VectorizedCnt *
4875             TTI->getMemoryOpCost(Instruction::Load, LoadTy, Alignment,
4876                                  LI->getPointerAddressSpace(), CostKind, LI);
4877         GatherCost += ScatterVectorizeCnt *
4878                       TTI->getGatherScatterOpCost(
4879                           Instruction::Load, LoadTy, LI->getPointerOperand(),
4880                           /*VariableMask=*/false, Alignment, CostKind, LI);
4881         if (NeedInsertSubvectorAnalysis) {
4882           // Add the cost for the subvectors insert.
4883           for (int I = VF, E = VL.size(); I < E; I += VF)
4884             GatherCost += TTI->getShuffleCost(TTI::SK_InsertSubvector, VecTy,
4885                                               None, I, LoadTy);
4886         }
4887         return ReuseShuffleCost + GatherCost - ScalarsCost;
4888       }
4889     }
4890     return ReuseShuffleCost + getGatherCost(VL);
4891   }
4892   InstructionCost CommonCost = 0;
4893   SmallVector<int> Mask;
4894   if (!E->ReorderIndices.empty()) {
4895     SmallVector<int> NewMask;
4896     if (E->getOpcode() == Instruction::Store) {
4897       // For stores the order is actually a mask.
4898       NewMask.resize(E->ReorderIndices.size());
4899       copy(E->ReorderIndices, NewMask.begin());
4900     } else {
4901       inversePermutation(E->ReorderIndices, NewMask);
4902     }
4903     ::addMask(Mask, NewMask);
4904   }
4905   if (NeedToShuffleReuses)
4906     ::addMask(Mask, E->ReuseShuffleIndices);
4907   if (!Mask.empty() && !ShuffleVectorInst::isIdentityMask(Mask))
4908     CommonCost =
4909         TTI->getShuffleCost(TTI::SK_PermuteSingleSrc, FinalVecTy, Mask);
4910   assert((E->State == TreeEntry::Vectorize ||
4911           E->State == TreeEntry::ScatterVectorize) &&
4912          "Unhandled state");
4913   assert(E->getOpcode() && allSameType(VL) && allSameBlock(VL) && "Invalid VL");
4914   Instruction *VL0 = E->getMainOp();
4915   unsigned ShuffleOrOp =
4916       E->isAltShuffle() ? (unsigned)Instruction::ShuffleVector : E->getOpcode();
4917   switch (ShuffleOrOp) {
4918     case Instruction::PHI:
4919       return 0;
4920 
4921     case Instruction::ExtractValue:
4922     case Instruction::ExtractElement: {
4923       // The common cost of removal ExtractElement/ExtractValue instructions +
4924       // the cost of shuffles, if required to resuffle the original vector.
4925       if (NeedToShuffleReuses) {
4926         unsigned Idx = 0;
4927         for (unsigned I : E->ReuseShuffleIndices) {
4928           if (ShuffleOrOp == Instruction::ExtractElement) {
4929             auto *EE = cast<ExtractElementInst>(VL[I]);
4930             CommonCost -= TTI->getVectorInstrCost(Instruction::ExtractElement,
4931                                                   EE->getVectorOperandType(),
4932                                                   *getExtractIndex(EE));
4933           } else {
4934             CommonCost -= TTI->getVectorInstrCost(Instruction::ExtractElement,
4935                                                   VecTy, Idx);
4936             ++Idx;
4937           }
4938         }
4939         Idx = EntryVF;
4940         for (Value *V : VL) {
4941           if (ShuffleOrOp == Instruction::ExtractElement) {
4942             auto *EE = cast<ExtractElementInst>(V);
4943             CommonCost += TTI->getVectorInstrCost(Instruction::ExtractElement,
4944                                                   EE->getVectorOperandType(),
4945                                                   *getExtractIndex(EE));
4946           } else {
4947             --Idx;
4948             CommonCost += TTI->getVectorInstrCost(Instruction::ExtractElement,
4949                                                   VecTy, Idx);
4950           }
4951         }
4952       }
4953       if (ShuffleOrOp == Instruction::ExtractValue) {
4954         for (unsigned I = 0, E = VL.size(); I < E; ++I) {
4955           auto *EI = cast<Instruction>(VL[I]);
4956           // Take credit for instruction that will become dead.
4957           if (EI->hasOneUse()) {
4958             Instruction *Ext = EI->user_back();
4959             if ((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
4960                 all_of(Ext->users(),
4961                        [](User *U) { return isa<GetElementPtrInst>(U); })) {
4962               // Use getExtractWithExtendCost() to calculate the cost of
4963               // extractelement/ext pair.
4964               CommonCost -= TTI->getExtractWithExtendCost(
4965                   Ext->getOpcode(), Ext->getType(), VecTy, I);
4966               // Add back the cost of s|zext which is subtracted separately.
4967               CommonCost += TTI->getCastInstrCost(
4968                   Ext->getOpcode(), Ext->getType(), EI->getType(),
4969                   TTI::getCastContextHint(Ext), CostKind, Ext);
4970               continue;
4971             }
4972           }
4973           CommonCost -=
4974               TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, I);
4975         }
4976       } else {
4977         AdjustExtractsCost(CommonCost);
4978       }
4979       return CommonCost;
4980     }
4981     case Instruction::InsertElement: {
4982       assert(E->ReuseShuffleIndices.empty() &&
4983              "Unique insertelements only are expected.");
4984       auto *SrcVecTy = cast<FixedVectorType>(VL0->getType());
4985 
4986       unsigned const NumElts = SrcVecTy->getNumElements();
4987       unsigned const NumScalars = VL.size();
4988       APInt DemandedElts = APInt::getZero(NumElts);
4989       // TODO: Add support for Instruction::InsertValue.
4990       SmallVector<int> Mask;
4991       if (!E->ReorderIndices.empty()) {
4992         inversePermutation(E->ReorderIndices, Mask);
4993         Mask.append(NumElts - NumScalars, UndefMaskElem);
4994       } else {
4995         Mask.assign(NumElts, UndefMaskElem);
4996         std::iota(Mask.begin(), std::next(Mask.begin(), NumScalars), 0);
4997       }
4998       unsigned Offset = *getInsertIndex(VL0, 0);
4999       bool IsIdentity = true;
5000       SmallVector<int> PrevMask(NumElts, UndefMaskElem);
5001       Mask.swap(PrevMask);
5002       for (unsigned I = 0; I < NumScalars; ++I) {
5003         Optional<int> InsertIdx = getInsertIndex(VL[PrevMask[I]], 0);
5004         if (!InsertIdx || *InsertIdx == UndefMaskElem)
5005           continue;
5006         DemandedElts.setBit(*InsertIdx);
5007         IsIdentity &= *InsertIdx - Offset == I;
5008         Mask[*InsertIdx - Offset] = I;
5009       }
5010       assert(Offset < NumElts && "Failed to find vector index offset");
5011 
5012       InstructionCost Cost = 0;
5013       Cost -= TTI->getScalarizationOverhead(SrcVecTy, DemandedElts,
5014                                             /*Insert*/ true, /*Extract*/ false);
5015 
5016       if (IsIdentity && NumElts != NumScalars && Offset % NumScalars != 0) {
5017         // FIXME: Replace with SK_InsertSubvector once it is properly supported.
5018         unsigned Sz = PowerOf2Ceil(Offset + NumScalars);
5019         Cost += TTI->getShuffleCost(
5020             TargetTransformInfo::SK_PermuteSingleSrc,
5021             FixedVectorType::get(SrcVecTy->getElementType(), Sz));
5022       } else if (!IsIdentity) {
5023         auto *FirstInsert =
5024             cast<Instruction>(*find_if(E->Scalars, [E](Value *V) {
5025               return !is_contained(E->Scalars,
5026                                    cast<Instruction>(V)->getOperand(0));
5027             }));
5028         if (isUndefVector(FirstInsert->getOperand(0))) {
5029           Cost += TTI->getShuffleCost(TTI::SK_PermuteSingleSrc, SrcVecTy, Mask);
5030         } else {
5031           SmallVector<int> InsertMask(NumElts);
5032           std::iota(InsertMask.begin(), InsertMask.end(), 0);
5033           for (unsigned I = 0; I < NumElts; I++) {
5034             if (Mask[I] != UndefMaskElem)
5035               InsertMask[Offset + I] = NumElts + I;
5036           }
5037           Cost +=
5038               TTI->getShuffleCost(TTI::SK_PermuteTwoSrc, SrcVecTy, InsertMask);
5039         }
5040       }
5041 
5042       return Cost;
5043     }
5044     case Instruction::ZExt:
5045     case Instruction::SExt:
5046     case Instruction::FPToUI:
5047     case Instruction::FPToSI:
5048     case Instruction::FPExt:
5049     case Instruction::PtrToInt:
5050     case Instruction::IntToPtr:
5051     case Instruction::SIToFP:
5052     case Instruction::UIToFP:
5053     case Instruction::Trunc:
5054     case Instruction::FPTrunc:
5055     case Instruction::BitCast: {
5056       Type *SrcTy = VL0->getOperand(0)->getType();
5057       InstructionCost ScalarEltCost =
5058           TTI->getCastInstrCost(E->getOpcode(), ScalarTy, SrcTy,
5059                                 TTI::getCastContextHint(VL0), CostKind, VL0);
5060       if (NeedToShuffleReuses) {
5061         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5062       }
5063 
5064       // Calculate the cost of this instruction.
5065       InstructionCost ScalarCost = VL.size() * ScalarEltCost;
5066 
5067       auto *SrcVecTy = FixedVectorType::get(SrcTy, VL.size());
5068       InstructionCost VecCost = 0;
5069       // Check if the values are candidates to demote.
5070       if (!MinBWs.count(VL0) || VecTy != SrcVecTy) {
5071         VecCost = CommonCost + TTI->getCastInstrCost(
5072                                    E->getOpcode(), VecTy, SrcVecTy,
5073                                    TTI::getCastContextHint(VL0), CostKind, VL0);
5074       }
5075       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost));
5076       return VecCost - ScalarCost;
5077     }
5078     case Instruction::FCmp:
5079     case Instruction::ICmp:
5080     case Instruction::Select: {
5081       // Calculate the cost of this instruction.
5082       InstructionCost ScalarEltCost =
5083           TTI->getCmpSelInstrCost(E->getOpcode(), ScalarTy, Builder.getInt1Ty(),
5084                                   CmpInst::BAD_ICMP_PREDICATE, CostKind, VL0);
5085       if (NeedToShuffleReuses) {
5086         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5087       }
5088       auto *MaskTy = FixedVectorType::get(Builder.getInt1Ty(), VL.size());
5089       InstructionCost ScalarCost = VecTy->getNumElements() * ScalarEltCost;
5090 
5091       // Check if all entries in VL are either compares or selects with compares
5092       // as condition that have the same predicates.
5093       CmpInst::Predicate VecPred = CmpInst::BAD_ICMP_PREDICATE;
5094       bool First = true;
5095       for (auto *V : VL) {
5096         CmpInst::Predicate CurrentPred;
5097         auto MatchCmp = m_Cmp(CurrentPred, m_Value(), m_Value());
5098         if ((!match(V, m_Select(MatchCmp, m_Value(), m_Value())) &&
5099              !match(V, MatchCmp)) ||
5100             (!First && VecPred != CurrentPred)) {
5101           VecPred = CmpInst::BAD_ICMP_PREDICATE;
5102           break;
5103         }
5104         First = false;
5105         VecPred = CurrentPred;
5106       }
5107 
5108       InstructionCost VecCost = TTI->getCmpSelInstrCost(
5109           E->getOpcode(), VecTy, MaskTy, VecPred, CostKind, VL0);
5110       // Check if it is possible and profitable to use min/max for selects in
5111       // VL.
5112       //
5113       auto IntrinsicAndUse = canConvertToMinOrMaxIntrinsic(VL);
5114       if (IntrinsicAndUse.first != Intrinsic::not_intrinsic) {
5115         IntrinsicCostAttributes CostAttrs(IntrinsicAndUse.first, VecTy,
5116                                           {VecTy, VecTy});
5117         InstructionCost IntrinsicCost =
5118             TTI->getIntrinsicInstrCost(CostAttrs, CostKind);
5119         // If the selects are the only uses of the compares, they will be dead
5120         // and we can adjust the cost by removing their cost.
5121         if (IntrinsicAndUse.second)
5122           IntrinsicCost -=
5123               TTI->getCmpSelInstrCost(Instruction::ICmp, VecTy, MaskTy,
5124                                       CmpInst::BAD_ICMP_PREDICATE, CostKind);
5125         VecCost = std::min(VecCost, IntrinsicCost);
5126       }
5127       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost));
5128       return CommonCost + VecCost - ScalarCost;
5129     }
5130     case Instruction::FNeg:
5131     case Instruction::Add:
5132     case Instruction::FAdd:
5133     case Instruction::Sub:
5134     case Instruction::FSub:
5135     case Instruction::Mul:
5136     case Instruction::FMul:
5137     case Instruction::UDiv:
5138     case Instruction::SDiv:
5139     case Instruction::FDiv:
5140     case Instruction::URem:
5141     case Instruction::SRem:
5142     case Instruction::FRem:
5143     case Instruction::Shl:
5144     case Instruction::LShr:
5145     case Instruction::AShr:
5146     case Instruction::And:
5147     case Instruction::Or:
5148     case Instruction::Xor: {
5149       // Certain instructions can be cheaper to vectorize if they have a
5150       // constant second vector operand.
5151       TargetTransformInfo::OperandValueKind Op1VK =
5152           TargetTransformInfo::OK_AnyValue;
5153       TargetTransformInfo::OperandValueKind Op2VK =
5154           TargetTransformInfo::OK_UniformConstantValue;
5155       TargetTransformInfo::OperandValueProperties Op1VP =
5156           TargetTransformInfo::OP_None;
5157       TargetTransformInfo::OperandValueProperties Op2VP =
5158           TargetTransformInfo::OP_PowerOf2;
5159 
5160       // If all operands are exactly the same ConstantInt then set the
5161       // operand kind to OK_UniformConstantValue.
5162       // If instead not all operands are constants, then set the operand kind
5163       // to OK_AnyValue. If all operands are constants but not the same,
5164       // then set the operand kind to OK_NonUniformConstantValue.
5165       ConstantInt *CInt0 = nullptr;
5166       for (unsigned i = 0, e = VL.size(); i < e; ++i) {
5167         const Instruction *I = cast<Instruction>(VL[i]);
5168         unsigned OpIdx = isa<BinaryOperator>(I) ? 1 : 0;
5169         ConstantInt *CInt = dyn_cast<ConstantInt>(I->getOperand(OpIdx));
5170         if (!CInt) {
5171           Op2VK = TargetTransformInfo::OK_AnyValue;
5172           Op2VP = TargetTransformInfo::OP_None;
5173           break;
5174         }
5175         if (Op2VP == TargetTransformInfo::OP_PowerOf2 &&
5176             !CInt->getValue().isPowerOf2())
5177           Op2VP = TargetTransformInfo::OP_None;
5178         if (i == 0) {
5179           CInt0 = CInt;
5180           continue;
5181         }
5182         if (CInt0 != CInt)
5183           Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
5184       }
5185 
5186       SmallVector<const Value *, 4> Operands(VL0->operand_values());
5187       InstructionCost ScalarEltCost =
5188           TTI->getArithmeticInstrCost(E->getOpcode(), ScalarTy, CostKind, Op1VK,
5189                                       Op2VK, Op1VP, Op2VP, Operands, VL0);
5190       if (NeedToShuffleReuses) {
5191         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5192       }
5193       InstructionCost ScalarCost = VecTy->getNumElements() * ScalarEltCost;
5194       InstructionCost VecCost =
5195           TTI->getArithmeticInstrCost(E->getOpcode(), VecTy, CostKind, Op1VK,
5196                                       Op2VK, Op1VP, Op2VP, Operands, VL0);
5197       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost));
5198       return CommonCost + VecCost - ScalarCost;
5199     }
5200     case Instruction::GetElementPtr: {
5201       TargetTransformInfo::OperandValueKind Op1VK =
5202           TargetTransformInfo::OK_AnyValue;
5203       TargetTransformInfo::OperandValueKind Op2VK =
5204           TargetTransformInfo::OK_UniformConstantValue;
5205 
5206       InstructionCost ScalarEltCost = TTI->getArithmeticInstrCost(
5207           Instruction::Add, ScalarTy, CostKind, Op1VK, Op2VK);
5208       if (NeedToShuffleReuses) {
5209         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5210       }
5211       InstructionCost ScalarCost = VecTy->getNumElements() * ScalarEltCost;
5212       InstructionCost VecCost = TTI->getArithmeticInstrCost(
5213           Instruction::Add, VecTy, CostKind, Op1VK, Op2VK);
5214       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost));
5215       return CommonCost + VecCost - ScalarCost;
5216     }
5217     case Instruction::Load: {
5218       // Cost of wide load - cost of scalar loads.
5219       Align Alignment = cast<LoadInst>(VL0)->getAlign();
5220       InstructionCost ScalarEltCost = TTI->getMemoryOpCost(
5221           Instruction::Load, ScalarTy, Alignment, 0, CostKind, VL0);
5222       if (NeedToShuffleReuses) {
5223         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5224       }
5225       InstructionCost ScalarLdCost = VecTy->getNumElements() * ScalarEltCost;
5226       InstructionCost VecLdCost;
5227       if (E->State == TreeEntry::Vectorize) {
5228         VecLdCost = TTI->getMemoryOpCost(Instruction::Load, VecTy, Alignment, 0,
5229                                          CostKind, VL0);
5230       } else {
5231         assert(E->State == TreeEntry::ScatterVectorize && "Unknown EntryState");
5232         Align CommonAlignment = Alignment;
5233         for (Value *V : VL)
5234           CommonAlignment =
5235               commonAlignment(CommonAlignment, cast<LoadInst>(V)->getAlign());
5236         VecLdCost = TTI->getGatherScatterOpCost(
5237             Instruction::Load, VecTy, cast<LoadInst>(VL0)->getPointerOperand(),
5238             /*VariableMask=*/false, CommonAlignment, CostKind, VL0);
5239       }
5240       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecLdCost, ScalarLdCost));
5241       return CommonCost + VecLdCost - ScalarLdCost;
5242     }
5243     case Instruction::Store: {
5244       // We know that we can merge the stores. Calculate the cost.
5245       bool IsReorder = !E->ReorderIndices.empty();
5246       auto *SI =
5247           cast<StoreInst>(IsReorder ? VL[E->ReorderIndices.front()] : VL0);
5248       Align Alignment = SI->getAlign();
5249       InstructionCost ScalarEltCost = TTI->getMemoryOpCost(
5250           Instruction::Store, ScalarTy, Alignment, 0, CostKind, VL0);
5251       InstructionCost ScalarStCost = VecTy->getNumElements() * ScalarEltCost;
5252       InstructionCost VecStCost = TTI->getMemoryOpCost(
5253           Instruction::Store, VecTy, Alignment, 0, CostKind, VL0);
5254       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecStCost, ScalarStCost));
5255       return CommonCost + VecStCost - ScalarStCost;
5256     }
5257     case Instruction::Call: {
5258       CallInst *CI = cast<CallInst>(VL0);
5259       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
5260 
5261       // Calculate the cost of the scalar and vector calls.
5262       IntrinsicCostAttributes CostAttrs(ID, *CI, 1);
5263       InstructionCost ScalarEltCost =
5264           TTI->getIntrinsicInstrCost(CostAttrs, CostKind);
5265       if (NeedToShuffleReuses) {
5266         CommonCost -= (EntryVF - VL.size()) * ScalarEltCost;
5267       }
5268       InstructionCost ScalarCallCost = VecTy->getNumElements() * ScalarEltCost;
5269 
5270       auto VecCallCosts = getVectorCallCosts(CI, VecTy, TTI, TLI);
5271       InstructionCost VecCallCost =
5272           std::min(VecCallCosts.first, VecCallCosts.second);
5273 
5274       LLVM_DEBUG(dbgs() << "SLP: Call cost " << VecCallCost - ScalarCallCost
5275                         << " (" << VecCallCost << "-" << ScalarCallCost << ")"
5276                         << " for " << *CI << "\n");
5277 
5278       return CommonCost + VecCallCost - ScalarCallCost;
5279     }
5280     case Instruction::ShuffleVector: {
5281       assert(E->isAltShuffle() &&
5282              ((Instruction::isBinaryOp(E->getOpcode()) &&
5283                Instruction::isBinaryOp(E->getAltOpcode())) ||
5284               (Instruction::isCast(E->getOpcode()) &&
5285                Instruction::isCast(E->getAltOpcode()))) &&
5286              "Invalid Shuffle Vector Operand");
5287       InstructionCost ScalarCost = 0;
5288       if (NeedToShuffleReuses) {
5289         for (unsigned Idx : E->ReuseShuffleIndices) {
5290           Instruction *I = cast<Instruction>(VL[Idx]);
5291           CommonCost -= TTI->getInstructionCost(I, CostKind);
5292         }
5293         for (Value *V : VL) {
5294           Instruction *I = cast<Instruction>(V);
5295           CommonCost += TTI->getInstructionCost(I, CostKind);
5296         }
5297       }
5298       for (Value *V : VL) {
5299         Instruction *I = cast<Instruction>(V);
5300         assert(E->isOpcodeOrAlt(I) && "Unexpected main/alternate opcode");
5301         ScalarCost += TTI->getInstructionCost(I, CostKind);
5302       }
5303       // VecCost is equal to sum of the cost of creating 2 vectors
5304       // and the cost of creating shuffle.
5305       InstructionCost VecCost = 0;
5306       // Try to find the previous shuffle node with the same operands and same
5307       // main/alternate ops.
5308       auto &&TryFindNodeWithEqualOperands = [this, E]() {
5309         for (const std::unique_ptr<TreeEntry> &TE : VectorizableTree) {
5310           if (TE.get() == E)
5311             break;
5312           if (TE->isAltShuffle() &&
5313               ((TE->getOpcode() == E->getOpcode() &&
5314                 TE->getAltOpcode() == E->getAltOpcode()) ||
5315                (TE->getOpcode() == E->getAltOpcode() &&
5316                 TE->getAltOpcode() == E->getOpcode())) &&
5317               TE->hasEqualOperands(*E))
5318             return true;
5319         }
5320         return false;
5321       };
5322       if (TryFindNodeWithEqualOperands()) {
5323         LLVM_DEBUG({
5324           dbgs() << "SLP: diamond match for alternate node found.\n";
5325           E->dump();
5326         });
5327         // No need to add new vector costs here since we're going to reuse
5328         // same main/alternate vector ops, just do different shuffling.
5329       } else if (Instruction::isBinaryOp(E->getOpcode())) {
5330         VecCost = TTI->getArithmeticInstrCost(E->getOpcode(), VecTy, CostKind);
5331         VecCost += TTI->getArithmeticInstrCost(E->getAltOpcode(), VecTy,
5332                                                CostKind);
5333       } else {
5334         Type *Src0SclTy = E->getMainOp()->getOperand(0)->getType();
5335         Type *Src1SclTy = E->getAltOp()->getOperand(0)->getType();
5336         auto *Src0Ty = FixedVectorType::get(Src0SclTy, VL.size());
5337         auto *Src1Ty = FixedVectorType::get(Src1SclTy, VL.size());
5338         VecCost = TTI->getCastInstrCost(E->getOpcode(), VecTy, Src0Ty,
5339                                         TTI::CastContextHint::None, CostKind);
5340         VecCost += TTI->getCastInstrCost(E->getAltOpcode(), VecTy, Src1Ty,
5341                                          TTI::CastContextHint::None, CostKind);
5342       }
5343 
5344       SmallVector<int> Mask;
5345       buildSuffleEntryMask(
5346           E->Scalars, E->ReorderIndices, E->ReuseShuffleIndices,
5347           [E](Instruction *I) {
5348             assert(E->isOpcodeOrAlt(I) && "Unexpected main/alternate opcode");
5349             return I->getOpcode() == E->getAltOpcode();
5350           },
5351           Mask);
5352       CommonCost =
5353           TTI->getShuffleCost(TargetTransformInfo::SK_Select, FinalVecTy, Mask);
5354       LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost));
5355       return CommonCost + VecCost - ScalarCost;
5356     }
5357     default:
5358       llvm_unreachable("Unknown instruction");
5359   }
5360 }
5361 
5362 bool BoUpSLP::isFullyVectorizableTinyTree(bool ForReduction) const {
5363   LLVM_DEBUG(dbgs() << "SLP: Check whether the tree with height "
5364                     << VectorizableTree.size() << " is fully vectorizable .\n");
5365 
5366   auto &&AreVectorizableGathers = [this](const TreeEntry *TE, unsigned Limit) {
5367     SmallVector<int> Mask;
5368     return TE->State == TreeEntry::NeedToGather &&
5369            !any_of(TE->Scalars,
5370                    [this](Value *V) { return EphValues.contains(V); }) &&
5371            (allConstant(TE->Scalars) || isSplat(TE->Scalars) ||
5372             TE->Scalars.size() < Limit ||
5373             ((TE->getOpcode() == Instruction::ExtractElement ||
5374               all_of(TE->Scalars,
5375                      [](Value *V) {
5376                        return isa<ExtractElementInst, UndefValue>(V);
5377                      })) &&
5378              isFixedVectorShuffle(TE->Scalars, Mask)) ||
5379             (TE->State == TreeEntry::NeedToGather &&
5380              TE->getOpcode() == Instruction::Load && !TE->isAltShuffle()));
5381   };
5382 
5383   // We only handle trees of heights 1 and 2.
5384   if (VectorizableTree.size() == 1 &&
5385       (VectorizableTree[0]->State == TreeEntry::Vectorize ||
5386        (ForReduction &&
5387         AreVectorizableGathers(VectorizableTree[0].get(),
5388                                VectorizableTree[0]->Scalars.size()) &&
5389         VectorizableTree[0]->getVectorFactor() > 2)))
5390     return true;
5391 
5392   if (VectorizableTree.size() != 2)
5393     return false;
5394 
5395   // Handle splat and all-constants stores. Also try to vectorize tiny trees
5396   // with the second gather nodes if they have less scalar operands rather than
5397   // the initial tree element (may be profitable to shuffle the second gather)
5398   // or they are extractelements, which form shuffle.
5399   SmallVector<int> Mask;
5400   if (VectorizableTree[0]->State == TreeEntry::Vectorize &&
5401       AreVectorizableGathers(VectorizableTree[1].get(),
5402                              VectorizableTree[0]->Scalars.size()))
5403     return true;
5404 
5405   // Gathering cost would be too much for tiny trees.
5406   if (VectorizableTree[0]->State == TreeEntry::NeedToGather ||
5407       (VectorizableTree[1]->State == TreeEntry::NeedToGather &&
5408        VectorizableTree[0]->State != TreeEntry::ScatterVectorize))
5409     return false;
5410 
5411   return true;
5412 }
5413 
5414 static bool isLoadCombineCandidateImpl(Value *Root, unsigned NumElts,
5415                                        TargetTransformInfo *TTI,
5416                                        bool MustMatchOrInst) {
5417   // Look past the root to find a source value. Arbitrarily follow the
5418   // path through operand 0 of any 'or'. Also, peek through optional
5419   // shift-left-by-multiple-of-8-bits.
5420   Value *ZextLoad = Root;
5421   const APInt *ShAmtC;
5422   bool FoundOr = false;
5423   while (!isa<ConstantExpr>(ZextLoad) &&
5424          (match(ZextLoad, m_Or(m_Value(), m_Value())) ||
5425           (match(ZextLoad, m_Shl(m_Value(), m_APInt(ShAmtC))) &&
5426            ShAmtC->urem(8) == 0))) {
5427     auto *BinOp = cast<BinaryOperator>(ZextLoad);
5428     ZextLoad = BinOp->getOperand(0);
5429     if (BinOp->getOpcode() == Instruction::Or)
5430       FoundOr = true;
5431   }
5432   // Check if the input is an extended load of the required or/shift expression.
5433   Value *Load;
5434   if ((MustMatchOrInst && !FoundOr) || ZextLoad == Root ||
5435       !match(ZextLoad, m_ZExt(m_Value(Load))) || !isa<LoadInst>(Load))
5436     return false;
5437 
5438   // Require that the total load bit width is a legal integer type.
5439   // For example, <8 x i8> --> i64 is a legal integer on a 64-bit target.
5440   // But <16 x i8> --> i128 is not, so the backend probably can't reduce it.
5441   Type *SrcTy = Load->getType();
5442   unsigned LoadBitWidth = SrcTy->getIntegerBitWidth() * NumElts;
5443   if (!TTI->isTypeLegal(IntegerType::get(Root->getContext(), LoadBitWidth)))
5444     return false;
5445 
5446   // Everything matched - assume that we can fold the whole sequence using
5447   // load combining.
5448   LLVM_DEBUG(dbgs() << "SLP: Assume load combining for tree starting at "
5449              << *(cast<Instruction>(Root)) << "\n");
5450 
5451   return true;
5452 }
5453 
5454 bool BoUpSLP::isLoadCombineReductionCandidate(RecurKind RdxKind) const {
5455   if (RdxKind != RecurKind::Or)
5456     return false;
5457 
5458   unsigned NumElts = VectorizableTree[0]->Scalars.size();
5459   Value *FirstReduced = VectorizableTree[0]->Scalars[0];
5460   return isLoadCombineCandidateImpl(FirstReduced, NumElts, TTI,
5461                                     /* MatchOr */ false);
5462 }
5463 
5464 bool BoUpSLP::isLoadCombineCandidate() const {
5465   // Peek through a final sequence of stores and check if all operations are
5466   // likely to be load-combined.
5467   unsigned NumElts = VectorizableTree[0]->Scalars.size();
5468   for (Value *Scalar : VectorizableTree[0]->Scalars) {
5469     Value *X;
5470     if (!match(Scalar, m_Store(m_Value(X), m_Value())) ||
5471         !isLoadCombineCandidateImpl(X, NumElts, TTI, /* MatchOr */ true))
5472       return false;
5473   }
5474   return true;
5475 }
5476 
5477 bool BoUpSLP::isTreeTinyAndNotFullyVectorizable(bool ForReduction) const {
5478   // No need to vectorize inserts of gathered values.
5479   if (VectorizableTree.size() == 2 &&
5480       isa<InsertElementInst>(VectorizableTree[0]->Scalars[0]) &&
5481       VectorizableTree[1]->State == TreeEntry::NeedToGather)
5482     return true;
5483 
5484   // We can vectorize the tree if its size is greater than or equal to the
5485   // minimum size specified by the MinTreeSize command line option.
5486   if (VectorizableTree.size() >= MinTreeSize)
5487     return false;
5488 
5489   // If we have a tiny tree (a tree whose size is less than MinTreeSize), we
5490   // can vectorize it if we can prove it fully vectorizable.
5491   if (isFullyVectorizableTinyTree(ForReduction))
5492     return false;
5493 
5494   assert(VectorizableTree.empty()
5495              ? ExternalUses.empty()
5496              : true && "We shouldn't have any external users");
5497 
5498   // Otherwise, we can't vectorize the tree. It is both tiny and not fully
5499   // vectorizable.
5500   return true;
5501 }
5502 
5503 InstructionCost BoUpSLP::getSpillCost() const {
5504   // Walk from the bottom of the tree to the top, tracking which values are
5505   // live. When we see a call instruction that is not part of our tree,
5506   // query TTI to see if there is a cost to keeping values live over it
5507   // (for example, if spills and fills are required).
5508   unsigned BundleWidth = VectorizableTree.front()->Scalars.size();
5509   InstructionCost Cost = 0;
5510 
5511   SmallPtrSet<Instruction*, 4> LiveValues;
5512   Instruction *PrevInst = nullptr;
5513 
5514   // The entries in VectorizableTree are not necessarily ordered by their
5515   // position in basic blocks. Collect them and order them by dominance so later
5516   // instructions are guaranteed to be visited first. For instructions in
5517   // different basic blocks, we only scan to the beginning of the block, so
5518   // their order does not matter, as long as all instructions in a basic block
5519   // are grouped together. Using dominance ensures a deterministic order.
5520   SmallVector<Instruction *, 16> OrderedScalars;
5521   for (const auto &TEPtr : VectorizableTree) {
5522     Instruction *Inst = dyn_cast<Instruction>(TEPtr->Scalars[0]);
5523     if (!Inst)
5524       continue;
5525     OrderedScalars.push_back(Inst);
5526   }
5527   llvm::sort(OrderedScalars, [&](Instruction *A, Instruction *B) {
5528     auto *NodeA = DT->getNode(A->getParent());
5529     auto *NodeB = DT->getNode(B->getParent());
5530     assert(NodeA && "Should only process reachable instructions");
5531     assert(NodeB && "Should only process reachable instructions");
5532     assert((NodeA == NodeB) == (NodeA->getDFSNumIn() == NodeB->getDFSNumIn()) &&
5533            "Different nodes should have different DFS numbers");
5534     if (NodeA != NodeB)
5535       return NodeA->getDFSNumIn() < NodeB->getDFSNumIn();
5536     return B->comesBefore(A);
5537   });
5538 
5539   for (Instruction *Inst : OrderedScalars) {
5540     if (!PrevInst) {
5541       PrevInst = Inst;
5542       continue;
5543     }
5544 
5545     // Update LiveValues.
5546     LiveValues.erase(PrevInst);
5547     for (auto &J : PrevInst->operands()) {
5548       if (isa<Instruction>(&*J) && getTreeEntry(&*J))
5549         LiveValues.insert(cast<Instruction>(&*J));
5550     }
5551 
5552     LLVM_DEBUG({
5553       dbgs() << "SLP: #LV: " << LiveValues.size();
5554       for (auto *X : LiveValues)
5555         dbgs() << " " << X->getName();
5556       dbgs() << ", Looking at ";
5557       Inst->dump();
5558     });
5559 
5560     // Now find the sequence of instructions between PrevInst and Inst.
5561     unsigned NumCalls = 0;
5562     BasicBlock::reverse_iterator InstIt = ++Inst->getIterator().getReverse(),
5563                                  PrevInstIt =
5564                                      PrevInst->getIterator().getReverse();
5565     while (InstIt != PrevInstIt) {
5566       if (PrevInstIt == PrevInst->getParent()->rend()) {
5567         PrevInstIt = Inst->getParent()->rbegin();
5568         continue;
5569       }
5570 
5571       // Debug information does not impact spill cost.
5572       if ((isa<CallInst>(&*PrevInstIt) &&
5573            !isa<DbgInfoIntrinsic>(&*PrevInstIt)) &&
5574           &*PrevInstIt != PrevInst)
5575         NumCalls++;
5576 
5577       ++PrevInstIt;
5578     }
5579 
5580     if (NumCalls) {
5581       SmallVector<Type*, 4> V;
5582       for (auto *II : LiveValues) {
5583         auto *ScalarTy = II->getType();
5584         if (auto *VectorTy = dyn_cast<FixedVectorType>(ScalarTy))
5585           ScalarTy = VectorTy->getElementType();
5586         V.push_back(FixedVectorType::get(ScalarTy, BundleWidth));
5587       }
5588       Cost += NumCalls * TTI->getCostOfKeepingLiveOverCall(V);
5589     }
5590 
5591     PrevInst = Inst;
5592   }
5593 
5594   return Cost;
5595 }
5596 
5597 /// Check if two insertelement instructions are from the same buildvector.
5598 static bool areTwoInsertFromSameBuildVector(InsertElementInst *VU,
5599                                             InsertElementInst *V) {
5600   // Instructions must be from the same basic blocks.
5601   if (VU->getParent() != V->getParent())
5602     return false;
5603   // Checks if 2 insertelements are from the same buildvector.
5604   if (VU->getType() != V->getType())
5605     return false;
5606   // Multiple used inserts are separate nodes.
5607   if (!VU->hasOneUse() && !V->hasOneUse())
5608     return false;
5609   auto *IE1 = VU;
5610   auto *IE2 = V;
5611   // Go through the vector operand of insertelement instructions trying to find
5612   // either VU as the original vector for IE2 or V as the original vector for
5613   // IE1.
5614   do {
5615     if (IE2 == VU || IE1 == V)
5616       return true;
5617     if (IE1) {
5618       if (IE1 != VU && !IE1->hasOneUse())
5619         IE1 = nullptr;
5620       else
5621         IE1 = dyn_cast<InsertElementInst>(IE1->getOperand(0));
5622     }
5623     if (IE2) {
5624       if (IE2 != V && !IE2->hasOneUse())
5625         IE2 = nullptr;
5626       else
5627         IE2 = dyn_cast<InsertElementInst>(IE2->getOperand(0));
5628     }
5629   } while (IE1 || IE2);
5630   return false;
5631 }
5632 
5633 InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
5634   InstructionCost Cost = 0;
5635   LLVM_DEBUG(dbgs() << "SLP: Calculating cost for tree of size "
5636                     << VectorizableTree.size() << ".\n");
5637 
5638   unsigned BundleWidth = VectorizableTree[0]->Scalars.size();
5639 
5640   for (unsigned I = 0, E = VectorizableTree.size(); I < E; ++I) {
5641     TreeEntry &TE = *VectorizableTree[I].get();
5642 
5643     InstructionCost C = getEntryCost(&TE, VectorizedVals);
5644     Cost += C;
5645     LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C
5646                       << " for bundle that starts with " << *TE.Scalars[0]
5647                       << ".\n"
5648                       << "SLP: Current total cost = " << Cost << "\n");
5649   }
5650 
5651   SmallPtrSet<Value *, 16> ExtractCostCalculated;
5652   InstructionCost ExtractCost = 0;
5653   SmallVector<unsigned> VF;
5654   SmallVector<SmallVector<int>> ShuffleMask;
5655   SmallVector<Value *> FirstUsers;
5656   SmallVector<APInt> DemandedElts;
5657   for (ExternalUser &EU : ExternalUses) {
5658     // We only add extract cost once for the same scalar.
5659     if (!isa_and_nonnull<InsertElementInst>(EU.User) &&
5660         !ExtractCostCalculated.insert(EU.Scalar).second)
5661       continue;
5662 
5663     // Uses by ephemeral values are free (because the ephemeral value will be
5664     // removed prior to code generation, and so the extraction will be
5665     // removed as well).
5666     if (EphValues.count(EU.User))
5667       continue;
5668 
5669     // No extract cost for vector "scalar"
5670     if (isa<FixedVectorType>(EU.Scalar->getType()))
5671       continue;
5672 
5673     // Already counted the cost for external uses when tried to adjust the cost
5674     // for extractelements, no need to add it again.
5675     if (isa<ExtractElementInst>(EU.Scalar))
5676       continue;
5677 
5678     // If found user is an insertelement, do not calculate extract cost but try
5679     // to detect it as a final shuffled/identity match.
5680     if (auto *VU = dyn_cast_or_null<InsertElementInst>(EU.User)) {
5681       if (auto *FTy = dyn_cast<FixedVectorType>(VU->getType())) {
5682         Optional<int> InsertIdx = getInsertIndex(VU, 0);
5683         if (!InsertIdx || *InsertIdx == UndefMaskElem)
5684           continue;
5685         auto *It = find_if(FirstUsers, [VU](Value *V) {
5686           return areTwoInsertFromSameBuildVector(VU,
5687                                                  cast<InsertElementInst>(V));
5688         });
5689         int VecId = -1;
5690         if (It == FirstUsers.end()) {
5691           VF.push_back(FTy->getNumElements());
5692           ShuffleMask.emplace_back(VF.back(), UndefMaskElem);
5693           // Find the insertvector, vectorized in tree, if any.
5694           Value *Base = VU;
5695           while (isa<InsertElementInst>(Base)) {
5696             // Build the mask for the vectorized insertelement instructions.
5697             if (const TreeEntry *E = getTreeEntry(Base)) {
5698               VU = cast<InsertElementInst>(Base);
5699               do {
5700                 int Idx = E->findLaneForValue(Base);
5701                 ShuffleMask.back()[Idx] = Idx;
5702                 Base = cast<InsertElementInst>(Base)->getOperand(0);
5703               } while (E == getTreeEntry(Base));
5704               break;
5705             }
5706             Base = cast<InsertElementInst>(Base)->getOperand(0);
5707           }
5708           FirstUsers.push_back(VU);
5709           DemandedElts.push_back(APInt::getZero(VF.back()));
5710           VecId = FirstUsers.size() - 1;
5711         } else {
5712           VecId = std::distance(FirstUsers.begin(), It);
5713         }
5714         int Idx = *InsertIdx;
5715         ShuffleMask[VecId][Idx] = EU.Lane;
5716         DemandedElts[VecId].setBit(Idx);
5717         continue;
5718       }
5719     }
5720 
5721     // If we plan to rewrite the tree in a smaller type, we will need to sign
5722     // extend the extracted value back to the original type. Here, we account
5723     // for the extract and the added cost of the sign extend if needed.
5724     auto *VecTy = FixedVectorType::get(EU.Scalar->getType(), BundleWidth);
5725     auto *ScalarRoot = VectorizableTree[0]->Scalars[0];
5726     if (MinBWs.count(ScalarRoot)) {
5727       auto *MinTy = IntegerType::get(F->getContext(), MinBWs[ScalarRoot].first);
5728       auto Extend =
5729           MinBWs[ScalarRoot].second ? Instruction::SExt : Instruction::ZExt;
5730       VecTy = FixedVectorType::get(MinTy, BundleWidth);
5731       ExtractCost += TTI->getExtractWithExtendCost(Extend, EU.Scalar->getType(),
5732                                                    VecTy, EU.Lane);
5733     } else {
5734       ExtractCost +=
5735           TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, EU.Lane);
5736     }
5737   }
5738 
5739   InstructionCost SpillCost = getSpillCost();
5740   Cost += SpillCost + ExtractCost;
5741   if (FirstUsers.size() == 1) {
5742     int Limit = ShuffleMask.front().size() * 2;
5743     if (all_of(ShuffleMask.front(), [Limit](int Idx) { return Idx < Limit; }) &&
5744         !ShuffleVectorInst::isIdentityMask(ShuffleMask.front())) {
5745       InstructionCost C = TTI->getShuffleCost(
5746           TTI::SK_PermuteSingleSrc,
5747           cast<FixedVectorType>(FirstUsers.front()->getType()),
5748           ShuffleMask.front());
5749       LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C
5750                         << " for final shuffle of insertelement external users "
5751                         << *VectorizableTree.front()->Scalars.front() << ".\n"
5752                         << "SLP: Current total cost = " << Cost << "\n");
5753       Cost += C;
5754     }
5755     InstructionCost InsertCost = TTI->getScalarizationOverhead(
5756         cast<FixedVectorType>(FirstUsers.front()->getType()),
5757         DemandedElts.front(), /*Insert*/ true, /*Extract*/ false);
5758     LLVM_DEBUG(dbgs() << "SLP: subtracting the cost " << InsertCost
5759                       << " for insertelements gather.\n"
5760                       << "SLP: Current total cost = " << Cost << "\n");
5761     Cost -= InsertCost;
5762   } else if (FirstUsers.size() >= 2) {
5763     unsigned MaxVF = *std::max_element(VF.begin(), VF.end());
5764     // Combined masks of the first 2 vectors.
5765     SmallVector<int> CombinedMask(MaxVF, UndefMaskElem);
5766     copy(ShuffleMask.front(), CombinedMask.begin());
5767     APInt CombinedDemandedElts = DemandedElts.front().zextOrSelf(MaxVF);
5768     auto *VecTy = FixedVectorType::get(
5769         cast<VectorType>(FirstUsers.front()->getType())->getElementType(),
5770         MaxVF);
5771     for (int I = 0, E = ShuffleMask[1].size(); I < E; ++I) {
5772       if (ShuffleMask[1][I] != UndefMaskElem) {
5773         CombinedMask[I] = ShuffleMask[1][I] + MaxVF;
5774         CombinedDemandedElts.setBit(I);
5775       }
5776     }
5777     InstructionCost C =
5778         TTI->getShuffleCost(TTI::SK_PermuteTwoSrc, VecTy, CombinedMask);
5779     LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C
5780                       << " for final shuffle of vector node and external "
5781                          "insertelement users "
5782                       << *VectorizableTree.front()->Scalars.front() << ".\n"
5783                       << "SLP: Current total cost = " << Cost << "\n");
5784     Cost += C;
5785     InstructionCost InsertCost = TTI->getScalarizationOverhead(
5786         VecTy, CombinedDemandedElts, /*Insert*/ true, /*Extract*/ false);
5787     LLVM_DEBUG(dbgs() << "SLP: subtracting the cost " << InsertCost
5788                       << " for insertelements gather.\n"
5789                       << "SLP: Current total cost = " << Cost << "\n");
5790     Cost -= InsertCost;
5791     for (int I = 2, E = FirstUsers.size(); I < E; ++I) {
5792       // Other elements - permutation of 2 vectors (the initial one and the
5793       // next Ith incoming vector).
5794       unsigned VF = ShuffleMask[I].size();
5795       for (unsigned Idx = 0; Idx < VF; ++Idx) {
5796         int Mask = ShuffleMask[I][Idx];
5797         if (Mask != UndefMaskElem)
5798           CombinedMask[Idx] = MaxVF + Mask;
5799         else if (CombinedMask[Idx] != UndefMaskElem)
5800           CombinedMask[Idx] = Idx;
5801       }
5802       for (unsigned Idx = VF; Idx < MaxVF; ++Idx)
5803         if (CombinedMask[Idx] != UndefMaskElem)
5804           CombinedMask[Idx] = Idx;
5805       InstructionCost C =
5806           TTI->getShuffleCost(TTI::SK_PermuteTwoSrc, VecTy, CombinedMask);
5807       LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C
5808                         << " for final shuffle of vector node and external "
5809                            "insertelement users "
5810                         << *VectorizableTree.front()->Scalars.front() << ".\n"
5811                         << "SLP: Current total cost = " << Cost << "\n");
5812       Cost += C;
5813       InstructionCost InsertCost = TTI->getScalarizationOverhead(
5814           cast<FixedVectorType>(FirstUsers[I]->getType()), DemandedElts[I],
5815           /*Insert*/ true, /*Extract*/ false);
5816       LLVM_DEBUG(dbgs() << "SLP: subtracting the cost " << InsertCost
5817                         << " for insertelements gather.\n"
5818                         << "SLP: Current total cost = " << Cost << "\n");
5819       Cost -= InsertCost;
5820     }
5821   }
5822 
5823 #ifndef NDEBUG
5824   SmallString<256> Str;
5825   {
5826     raw_svector_ostream OS(Str);
5827     OS << "SLP: Spill Cost = " << SpillCost << ".\n"
5828        << "SLP: Extract Cost = " << ExtractCost << ".\n"
5829        << "SLP: Total Cost = " << Cost << ".\n";
5830   }
5831   LLVM_DEBUG(dbgs() << Str);
5832   if (ViewSLPTree)
5833     ViewGraph(this, "SLP" + F->getName(), false, Str);
5834 #endif
5835 
5836   return Cost;
5837 }
5838 
5839 Optional<TargetTransformInfo::ShuffleKind>
5840 BoUpSLP::isGatherShuffledEntry(const TreeEntry *TE, SmallVectorImpl<int> &Mask,
5841                                SmallVectorImpl<const TreeEntry *> &Entries) {
5842   // TODO: currently checking only for Scalars in the tree entry, need to count
5843   // reused elements too for better cost estimation.
5844   Mask.assign(TE->Scalars.size(), UndefMaskElem);
5845   Entries.clear();
5846   // Build a lists of values to tree entries.
5847   DenseMap<Value *, SmallPtrSet<const TreeEntry *, 4>> ValueToTEs;
5848   for (const std::unique_ptr<TreeEntry> &EntryPtr : VectorizableTree) {
5849     if (EntryPtr.get() == TE)
5850       break;
5851     if (EntryPtr->State != TreeEntry::NeedToGather)
5852       continue;
5853     for (Value *V : EntryPtr->Scalars)
5854       ValueToTEs.try_emplace(V).first->getSecond().insert(EntryPtr.get());
5855   }
5856   // Find all tree entries used by the gathered values. If no common entries
5857   // found - not a shuffle.
5858   // Here we build a set of tree nodes for each gathered value and trying to
5859   // find the intersection between these sets. If we have at least one common
5860   // tree node for each gathered value - we have just a permutation of the
5861   // single vector. If we have 2 different sets, we're in situation where we
5862   // have a permutation of 2 input vectors.
5863   SmallVector<SmallPtrSet<const TreeEntry *, 4>> UsedTEs;
5864   DenseMap<Value *, int> UsedValuesEntry;
5865   for (Value *V : TE->Scalars) {
5866     if (isa<UndefValue>(V))
5867       continue;
5868     // Build a list of tree entries where V is used.
5869     SmallPtrSet<const TreeEntry *, 4> VToTEs;
5870     auto It = ValueToTEs.find(V);
5871     if (It != ValueToTEs.end())
5872       VToTEs = It->second;
5873     if (const TreeEntry *VTE = getTreeEntry(V))
5874       VToTEs.insert(VTE);
5875     if (VToTEs.empty())
5876       return None;
5877     if (UsedTEs.empty()) {
5878       // The first iteration, just insert the list of nodes to vector.
5879       UsedTEs.push_back(VToTEs);
5880     } else {
5881       // Need to check if there are any previously used tree nodes which use V.
5882       // If there are no such nodes, consider that we have another one input
5883       // vector.
5884       SmallPtrSet<const TreeEntry *, 4> SavedVToTEs(VToTEs);
5885       unsigned Idx = 0;
5886       for (SmallPtrSet<const TreeEntry *, 4> &Set : UsedTEs) {
5887         // Do we have a non-empty intersection of previously listed tree entries
5888         // and tree entries using current V?
5889         set_intersect(VToTEs, Set);
5890         if (!VToTEs.empty()) {
5891           // Yes, write the new subset and continue analysis for the next
5892           // scalar.
5893           Set.swap(VToTEs);
5894           break;
5895         }
5896         VToTEs = SavedVToTEs;
5897         ++Idx;
5898       }
5899       // No non-empty intersection found - need to add a second set of possible
5900       // source vectors.
5901       if (Idx == UsedTEs.size()) {
5902         // If the number of input vectors is greater than 2 - not a permutation,
5903         // fallback to the regular gather.
5904         if (UsedTEs.size() == 2)
5905           return None;
5906         UsedTEs.push_back(SavedVToTEs);
5907         Idx = UsedTEs.size() - 1;
5908       }
5909       UsedValuesEntry.try_emplace(V, Idx);
5910     }
5911   }
5912 
5913   unsigned VF = 0;
5914   if (UsedTEs.size() == 1) {
5915     // Try to find the perfect match in another gather node at first.
5916     auto It = find_if(UsedTEs.front(), [TE](const TreeEntry *EntryPtr) {
5917       return EntryPtr->isSame(TE->Scalars);
5918     });
5919     if (It != UsedTEs.front().end()) {
5920       Entries.push_back(*It);
5921       std::iota(Mask.begin(), Mask.end(), 0);
5922       return TargetTransformInfo::SK_PermuteSingleSrc;
5923     }
5924     // No perfect match, just shuffle, so choose the first tree node.
5925     Entries.push_back(*UsedTEs.front().begin());
5926   } else {
5927     // Try to find nodes with the same vector factor.
5928     assert(UsedTEs.size() == 2 && "Expected at max 2 permuted entries.");
5929     DenseMap<int, const TreeEntry *> VFToTE;
5930     for (const TreeEntry *TE : UsedTEs.front())
5931       VFToTE.try_emplace(TE->getVectorFactor(), TE);
5932     for (const TreeEntry *TE : UsedTEs.back()) {
5933       auto It = VFToTE.find(TE->getVectorFactor());
5934       if (It != VFToTE.end()) {
5935         VF = It->first;
5936         Entries.push_back(It->second);
5937         Entries.push_back(TE);
5938         break;
5939       }
5940     }
5941     // No 2 source vectors with the same vector factor - give up and do regular
5942     // gather.
5943     if (Entries.empty())
5944       return None;
5945   }
5946 
5947   // Build a shuffle mask for better cost estimation and vector emission.
5948   for (int I = 0, E = TE->Scalars.size(); I < E; ++I) {
5949     Value *V = TE->Scalars[I];
5950     if (isa<UndefValue>(V))
5951       continue;
5952     unsigned Idx = UsedValuesEntry.lookup(V);
5953     const TreeEntry *VTE = Entries[Idx];
5954     int FoundLane = VTE->findLaneForValue(V);
5955     Mask[I] = Idx * VF + FoundLane;
5956     // Extra check required by isSingleSourceMaskImpl function (called by
5957     // ShuffleVectorInst::isSingleSourceMask).
5958     if (Mask[I] >= 2 * E)
5959       return None;
5960   }
5961   switch (Entries.size()) {
5962   case 1:
5963     return TargetTransformInfo::SK_PermuteSingleSrc;
5964   case 2:
5965     return TargetTransformInfo::SK_PermuteTwoSrc;
5966   default:
5967     break;
5968   }
5969   return None;
5970 }
5971 
5972 InstructionCost
5973 BoUpSLP::getGatherCost(FixedVectorType *Ty,
5974                        const DenseSet<unsigned> &ShuffledIndices,
5975                        bool NeedToShuffle) const {
5976   unsigned NumElts = Ty->getNumElements();
5977   APInt DemandedElts = APInt::getZero(NumElts);
5978   for (unsigned I = 0; I < NumElts; ++I)
5979     if (!ShuffledIndices.count(I))
5980       DemandedElts.setBit(I);
5981   InstructionCost Cost =
5982       TTI->getScalarizationOverhead(Ty, DemandedElts, /*Insert*/ true,
5983                                     /*Extract*/ false);
5984   if (NeedToShuffle)
5985     Cost += TTI->getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, Ty);
5986   return Cost;
5987 }
5988 
5989 InstructionCost BoUpSLP::getGatherCost(ArrayRef<Value *> VL) const {
5990   // Find the type of the operands in VL.
5991   Type *ScalarTy = VL[0]->getType();
5992   if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
5993     ScalarTy = SI->getValueOperand()->getType();
5994   auto *VecTy = FixedVectorType::get(ScalarTy, VL.size());
5995   bool DuplicateNonConst = false;
5996   // Find the cost of inserting/extracting values from the vector.
5997   // Check if the same elements are inserted several times and count them as
5998   // shuffle candidates.
5999   DenseSet<unsigned> ShuffledElements;
6000   DenseSet<Value *> UniqueElements;
6001   // Iterate in reverse order to consider insert elements with the high cost.
6002   for (unsigned I = VL.size(); I > 0; --I) {
6003     unsigned Idx = I - 1;
6004     // No need to shuffle duplicates for constants.
6005     if (isConstant(VL[Idx])) {
6006       ShuffledElements.insert(Idx);
6007       continue;
6008     }
6009     if (!UniqueElements.insert(VL[Idx]).second) {
6010       DuplicateNonConst = true;
6011       ShuffledElements.insert(Idx);
6012     }
6013   }
6014   return getGatherCost(VecTy, ShuffledElements, DuplicateNonConst);
6015 }
6016 
6017 // Perform operand reordering on the instructions in VL and return the reordered
6018 // operands in Left and Right.
6019 void BoUpSLP::reorderInputsAccordingToOpcode(ArrayRef<Value *> VL,
6020                                              SmallVectorImpl<Value *> &Left,
6021                                              SmallVectorImpl<Value *> &Right,
6022                                              const DataLayout &DL,
6023                                              ScalarEvolution &SE,
6024                                              const BoUpSLP &R) {
6025   if (VL.empty())
6026     return;
6027   VLOperands Ops(VL, DL, SE, R);
6028   // Reorder the operands in place.
6029   Ops.reorder();
6030   Left = Ops.getVL(0);
6031   Right = Ops.getVL(1);
6032 }
6033 
6034 void BoUpSLP::setInsertPointAfterBundle(const TreeEntry *E) {
6035   // Get the basic block this bundle is in. All instructions in the bundle
6036   // should be in this block.
6037   auto *Front = E->getMainOp();
6038   auto *BB = Front->getParent();
6039   assert(llvm::all_of(E->Scalars, [=](Value *V) -> bool {
6040     auto *I = cast<Instruction>(V);
6041     return !E->isOpcodeOrAlt(I) || I->getParent() == BB;
6042   }));
6043 
6044   // The last instruction in the bundle in program order.
6045   Instruction *LastInst = nullptr;
6046 
6047   // Find the last instruction. The common case should be that BB has been
6048   // scheduled, and the last instruction is VL.back(). So we start with
6049   // VL.back() and iterate over schedule data until we reach the end of the
6050   // bundle. The end of the bundle is marked by null ScheduleData.
6051   if (BlocksSchedules.count(BB)) {
6052     auto *Bundle =
6053         BlocksSchedules[BB]->getScheduleData(E->isOneOf(E->Scalars.back()));
6054     if (Bundle && Bundle->isPartOfBundle())
6055       for (; Bundle; Bundle = Bundle->NextInBundle)
6056         if (Bundle->OpValue == Bundle->Inst)
6057           LastInst = Bundle->Inst;
6058   }
6059 
6060   // LastInst can still be null at this point if there's either not an entry
6061   // for BB in BlocksSchedules or there's no ScheduleData available for
6062   // VL.back(). This can be the case if buildTree_rec aborts for various
6063   // reasons (e.g., the maximum recursion depth is reached, the maximum region
6064   // size is reached, etc.). ScheduleData is initialized in the scheduling
6065   // "dry-run".
6066   //
6067   // If this happens, we can still find the last instruction by brute force. We
6068   // iterate forwards from Front (inclusive) until we either see all
6069   // instructions in the bundle or reach the end of the block. If Front is the
6070   // last instruction in program order, LastInst will be set to Front, and we
6071   // will visit all the remaining instructions in the block.
6072   //
6073   // One of the reasons we exit early from buildTree_rec is to place an upper
6074   // bound on compile-time. Thus, taking an additional compile-time hit here is
6075   // not ideal. However, this should be exceedingly rare since it requires that
6076   // we both exit early from buildTree_rec and that the bundle be out-of-order
6077   // (causing us to iterate all the way to the end of the block).
6078   if (!LastInst) {
6079     SmallPtrSet<Value *, 16> Bundle(E->Scalars.begin(), E->Scalars.end());
6080     for (auto &I : make_range(BasicBlock::iterator(Front), BB->end())) {
6081       if (Bundle.erase(&I) && E->isOpcodeOrAlt(&I))
6082         LastInst = &I;
6083       if (Bundle.empty())
6084         break;
6085     }
6086   }
6087   assert(LastInst && "Failed to find last instruction in bundle");
6088 
6089   // Set the insertion point after the last instruction in the bundle. Set the
6090   // debug location to Front.
6091   Builder.SetInsertPoint(BB, ++LastInst->getIterator());
6092   Builder.SetCurrentDebugLocation(Front->getDebugLoc());
6093 }
6094 
6095 Value *BoUpSLP::gather(ArrayRef<Value *> VL) {
6096   // List of instructions/lanes from current block and/or the blocks which are
6097   // part of the current loop. These instructions will be inserted at the end to
6098   // make it possible to optimize loops and hoist invariant instructions out of
6099   // the loops body with better chances for success.
6100   SmallVector<std::pair<Value *, unsigned>, 4> PostponedInsts;
6101   SmallSet<int, 4> PostponedIndices;
6102   Loop *L = LI->getLoopFor(Builder.GetInsertBlock());
6103   auto &&CheckPredecessor = [](BasicBlock *InstBB, BasicBlock *InsertBB) {
6104     SmallPtrSet<BasicBlock *, 4> Visited;
6105     while (InsertBB && InsertBB != InstBB && Visited.insert(InsertBB).second)
6106       InsertBB = InsertBB->getSinglePredecessor();
6107     return InsertBB && InsertBB == InstBB;
6108   };
6109   for (int I = 0, E = VL.size(); I < E; ++I) {
6110     if (auto *Inst = dyn_cast<Instruction>(VL[I]))
6111       if ((CheckPredecessor(Inst->getParent(), Builder.GetInsertBlock()) ||
6112            getTreeEntry(Inst) || (L && (L->contains(Inst)))) &&
6113           PostponedIndices.insert(I).second)
6114         PostponedInsts.emplace_back(Inst, I);
6115   }
6116 
6117   auto &&CreateInsertElement = [this](Value *Vec, Value *V, unsigned Pos) {
6118     Vec = Builder.CreateInsertElement(Vec, V, Builder.getInt32(Pos));
6119     auto *InsElt = dyn_cast<InsertElementInst>(Vec);
6120     if (!InsElt)
6121       return Vec;
6122     GatherShuffleSeq.insert(InsElt);
6123     CSEBlocks.insert(InsElt->getParent());
6124     // Add to our 'need-to-extract' list.
6125     if (TreeEntry *Entry = getTreeEntry(V)) {
6126       // Find which lane we need to extract.
6127       unsigned FoundLane = Entry->findLaneForValue(V);
6128       ExternalUses.emplace_back(V, InsElt, FoundLane);
6129     }
6130     return Vec;
6131   };
6132   Value *Val0 =
6133       isa<StoreInst>(VL[0]) ? cast<StoreInst>(VL[0])->getValueOperand() : VL[0];
6134   FixedVectorType *VecTy = FixedVectorType::get(Val0->getType(), VL.size());
6135   Value *Vec = PoisonValue::get(VecTy);
6136   SmallVector<int> NonConsts;
6137   // Insert constant values at first.
6138   for (int I = 0, E = VL.size(); I < E; ++I) {
6139     if (PostponedIndices.contains(I))
6140       continue;
6141     if (!isConstant(VL[I])) {
6142       NonConsts.push_back(I);
6143       continue;
6144     }
6145     Vec = CreateInsertElement(Vec, VL[I], I);
6146   }
6147   // Insert non-constant values.
6148   for (int I : NonConsts)
6149     Vec = CreateInsertElement(Vec, VL[I], I);
6150   // Append instructions, which are/may be part of the loop, in the end to make
6151   // it possible to hoist non-loop-based instructions.
6152   for (const std::pair<Value *, unsigned> &Pair : PostponedInsts)
6153     Vec = CreateInsertElement(Vec, Pair.first, Pair.second);
6154 
6155   return Vec;
6156 }
6157 
6158 namespace {
6159 /// Merges shuffle masks and emits final shuffle instruction, if required.
6160 class ShuffleInstructionBuilder {
6161   IRBuilderBase &Builder;
6162   const unsigned VF = 0;
6163   bool IsFinalized = false;
6164   SmallVector<int, 4> Mask;
6165   /// Holds all of the instructions that we gathered.
6166   SetVector<Instruction *> &GatherShuffleSeq;
6167   /// A list of blocks that we are going to CSE.
6168   SetVector<BasicBlock *> &CSEBlocks;
6169 
6170 public:
6171   ShuffleInstructionBuilder(IRBuilderBase &Builder, unsigned VF,
6172                             SetVector<Instruction *> &GatherShuffleSeq,
6173                             SetVector<BasicBlock *> &CSEBlocks)
6174       : Builder(Builder), VF(VF), GatherShuffleSeq(GatherShuffleSeq),
6175         CSEBlocks(CSEBlocks) {}
6176 
6177   /// Adds a mask, inverting it before applying.
6178   void addInversedMask(ArrayRef<unsigned> SubMask) {
6179     if (SubMask.empty())
6180       return;
6181     SmallVector<int, 4> NewMask;
6182     inversePermutation(SubMask, NewMask);
6183     addMask(NewMask);
6184   }
6185 
6186   /// Functions adds masks, merging them into  single one.
6187   void addMask(ArrayRef<unsigned> SubMask) {
6188     SmallVector<int, 4> NewMask(SubMask.begin(), SubMask.end());
6189     addMask(NewMask);
6190   }
6191 
6192   void addMask(ArrayRef<int> SubMask) { ::addMask(Mask, SubMask); }
6193 
6194   Value *finalize(Value *V) {
6195     IsFinalized = true;
6196     unsigned ValueVF = cast<FixedVectorType>(V->getType())->getNumElements();
6197     if (VF == ValueVF && Mask.empty())
6198       return V;
6199     SmallVector<int, 4> NormalizedMask(VF, UndefMaskElem);
6200     std::iota(NormalizedMask.begin(), NormalizedMask.end(), 0);
6201     addMask(NormalizedMask);
6202 
6203     if (VF == ValueVF && ShuffleVectorInst::isIdentityMask(Mask))
6204       return V;
6205     Value *Vec = Builder.CreateShuffleVector(V, Mask, "shuffle");
6206     if (auto *I = dyn_cast<Instruction>(Vec)) {
6207       GatherShuffleSeq.insert(I);
6208       CSEBlocks.insert(I->getParent());
6209     }
6210     return Vec;
6211   }
6212 
6213   ~ShuffleInstructionBuilder() {
6214     assert((IsFinalized || Mask.empty()) &&
6215            "Shuffle construction must be finalized.");
6216   }
6217 };
6218 } // namespace
6219 
6220 Value *BoUpSLP::vectorizeTree(ArrayRef<Value *> VL) {
6221   unsigned VF = VL.size();
6222   InstructionsState S = getSameOpcode(VL);
6223   if (S.getOpcode()) {
6224     if (TreeEntry *E = getTreeEntry(S.OpValue))
6225       if (E->isSame(VL)) {
6226         Value *V = vectorizeTree(E);
6227         if (VF != cast<FixedVectorType>(V->getType())->getNumElements()) {
6228           if (!E->ReuseShuffleIndices.empty()) {
6229             // Reshuffle to get only unique values.
6230             // If some of the scalars are duplicated in the vectorization tree
6231             // entry, we do not vectorize them but instead generate a mask for
6232             // the reuses. But if there are several users of the same entry,
6233             // they may have different vectorization factors. This is especially
6234             // important for PHI nodes. In this case, we need to adapt the
6235             // resulting instruction for the user vectorization factor and have
6236             // to reshuffle it again to take only unique elements of the vector.
6237             // Without this code the function incorrectly returns reduced vector
6238             // instruction with the same elements, not with the unique ones.
6239 
6240             // block:
6241             // %phi = phi <2 x > { .., %entry} {%shuffle, %block}
6242             // %2 = shuffle <2 x > %phi, poison, <4 x > <1, 1, 0, 0>
6243             // ... (use %2)
6244             // %shuffle = shuffle <2 x> %2, poison, <2 x> {2, 0}
6245             // br %block
6246             SmallVector<int> UniqueIdxs(VF, UndefMaskElem);
6247             SmallSet<int, 4> UsedIdxs;
6248             int Pos = 0;
6249             int Sz = VL.size();
6250             for (int Idx : E->ReuseShuffleIndices) {
6251               if (Idx != Sz && Idx != UndefMaskElem &&
6252                   UsedIdxs.insert(Idx).second)
6253                 UniqueIdxs[Idx] = Pos;
6254               ++Pos;
6255             }
6256             assert(VF >= UsedIdxs.size() && "Expected vectorization factor "
6257                                             "less than original vector size.");
6258             UniqueIdxs.append(VF - UsedIdxs.size(), UndefMaskElem);
6259             V = Builder.CreateShuffleVector(V, UniqueIdxs, "shrink.shuffle");
6260           } else {
6261             assert(VF < cast<FixedVectorType>(V->getType())->getNumElements() &&
6262                    "Expected vectorization factor less "
6263                    "than original vector size.");
6264             SmallVector<int> UniformMask(VF, 0);
6265             std::iota(UniformMask.begin(), UniformMask.end(), 0);
6266             V = Builder.CreateShuffleVector(V, UniformMask, "shrink.shuffle");
6267           }
6268           if (auto *I = dyn_cast<Instruction>(V)) {
6269             GatherShuffleSeq.insert(I);
6270             CSEBlocks.insert(I->getParent());
6271           }
6272         }
6273         return V;
6274       }
6275   }
6276 
6277   // Check that every instruction appears once in this bundle.
6278   SmallVector<int> ReuseShuffleIndicies;
6279   SmallVector<Value *> UniqueValues;
6280   if (VL.size() > 2) {
6281     DenseMap<Value *, unsigned> UniquePositions;
6282     unsigned NumValues =
6283         std::distance(VL.begin(), find_if(reverse(VL), [](Value *V) {
6284                                     return !isa<UndefValue>(V);
6285                                   }).base());
6286     VF = std::max<unsigned>(VF, PowerOf2Ceil(NumValues));
6287     int UniqueVals = 0;
6288     for (Value *V : VL.drop_back(VL.size() - VF)) {
6289       if (isa<UndefValue>(V)) {
6290         ReuseShuffleIndicies.emplace_back(UndefMaskElem);
6291         continue;
6292       }
6293       if (isConstant(V)) {
6294         ReuseShuffleIndicies.emplace_back(UniqueValues.size());
6295         UniqueValues.emplace_back(V);
6296         continue;
6297       }
6298       auto Res = UniquePositions.try_emplace(V, UniqueValues.size());
6299       ReuseShuffleIndicies.emplace_back(Res.first->second);
6300       if (Res.second) {
6301         UniqueValues.emplace_back(V);
6302         ++UniqueVals;
6303       }
6304     }
6305     if (UniqueVals == 1 && UniqueValues.size() == 1) {
6306       // Emit pure splat vector.
6307       ReuseShuffleIndicies.append(VF - ReuseShuffleIndicies.size(),
6308                                   UndefMaskElem);
6309     } else if (UniqueValues.size() >= VF - 1 || UniqueValues.size() <= 1) {
6310       ReuseShuffleIndicies.clear();
6311       UniqueValues.clear();
6312       UniqueValues.append(VL.begin(), std::next(VL.begin(), NumValues));
6313     }
6314     UniqueValues.append(VF - UniqueValues.size(),
6315                         PoisonValue::get(VL[0]->getType()));
6316     VL = UniqueValues;
6317   }
6318 
6319   ShuffleInstructionBuilder ShuffleBuilder(Builder, VF, GatherShuffleSeq,
6320                                            CSEBlocks);
6321   Value *Vec = gather(VL);
6322   if (!ReuseShuffleIndicies.empty()) {
6323     ShuffleBuilder.addMask(ReuseShuffleIndicies);
6324     Vec = ShuffleBuilder.finalize(Vec);
6325   }
6326   return Vec;
6327 }
6328 
6329 Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
6330   IRBuilder<>::InsertPointGuard Guard(Builder);
6331 
6332   if (E->VectorizedValue) {
6333     LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *E->Scalars[0] << ".\n");
6334     return E->VectorizedValue;
6335   }
6336 
6337   bool NeedToShuffleReuses = !E->ReuseShuffleIndices.empty();
6338   unsigned VF = E->getVectorFactor();
6339   ShuffleInstructionBuilder ShuffleBuilder(Builder, VF, GatherShuffleSeq,
6340                                            CSEBlocks);
6341   if (E->State == TreeEntry::NeedToGather) {
6342     if (E->getMainOp())
6343       setInsertPointAfterBundle(E);
6344     Value *Vec;
6345     SmallVector<int> Mask;
6346     SmallVector<const TreeEntry *> Entries;
6347     Optional<TargetTransformInfo::ShuffleKind> Shuffle =
6348         isGatherShuffledEntry(E, Mask, Entries);
6349     if (Shuffle.hasValue()) {
6350       assert((Entries.size() == 1 || Entries.size() == 2) &&
6351              "Expected shuffle of 1 or 2 entries.");
6352       Vec = Builder.CreateShuffleVector(Entries.front()->VectorizedValue,
6353                                         Entries.back()->VectorizedValue, Mask);
6354       if (auto *I = dyn_cast<Instruction>(Vec)) {
6355         GatherShuffleSeq.insert(I);
6356         CSEBlocks.insert(I->getParent());
6357       }
6358     } else {
6359       Vec = gather(E->Scalars);
6360     }
6361     if (NeedToShuffleReuses) {
6362       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6363       Vec = ShuffleBuilder.finalize(Vec);
6364     }
6365     E->VectorizedValue = Vec;
6366     return Vec;
6367   }
6368 
6369   assert((E->State == TreeEntry::Vectorize ||
6370           E->State == TreeEntry::ScatterVectorize) &&
6371          "Unhandled state");
6372   unsigned ShuffleOrOp =
6373       E->isAltShuffle() ? (unsigned)Instruction::ShuffleVector : E->getOpcode();
6374   Instruction *VL0 = E->getMainOp();
6375   Type *ScalarTy = VL0->getType();
6376   if (auto *Store = dyn_cast<StoreInst>(VL0))
6377     ScalarTy = Store->getValueOperand()->getType();
6378   else if (auto *IE = dyn_cast<InsertElementInst>(VL0))
6379     ScalarTy = IE->getOperand(1)->getType();
6380   auto *VecTy = FixedVectorType::get(ScalarTy, E->Scalars.size());
6381   switch (ShuffleOrOp) {
6382     case Instruction::PHI: {
6383       assert(
6384           (E->ReorderIndices.empty() || E != VectorizableTree.front().get()) &&
6385           "PHI reordering is free.");
6386       auto *PH = cast<PHINode>(VL0);
6387       Builder.SetInsertPoint(PH->getParent()->getFirstNonPHI());
6388       Builder.SetCurrentDebugLocation(PH->getDebugLoc());
6389       PHINode *NewPhi = Builder.CreatePHI(VecTy, PH->getNumIncomingValues());
6390       Value *V = NewPhi;
6391       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6392       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6393       V = ShuffleBuilder.finalize(V);
6394 
6395       E->VectorizedValue = V;
6396 
6397       // PHINodes may have multiple entries from the same block. We want to
6398       // visit every block once.
6399       SmallPtrSet<BasicBlock*, 4> VisitedBBs;
6400 
6401       for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
6402         ValueList Operands;
6403         BasicBlock *IBB = PH->getIncomingBlock(i);
6404 
6405         if (!VisitedBBs.insert(IBB).second) {
6406           NewPhi->addIncoming(NewPhi->getIncomingValueForBlock(IBB), IBB);
6407           continue;
6408         }
6409 
6410         Builder.SetInsertPoint(IBB->getTerminator());
6411         Builder.SetCurrentDebugLocation(PH->getDebugLoc());
6412         Value *Vec = vectorizeTree(E->getOperand(i));
6413         NewPhi->addIncoming(Vec, IBB);
6414       }
6415 
6416       assert(NewPhi->getNumIncomingValues() == PH->getNumIncomingValues() &&
6417              "Invalid number of incoming values");
6418       return V;
6419     }
6420 
6421     case Instruction::ExtractElement: {
6422       Value *V = E->getSingleOperand(0);
6423       Builder.SetInsertPoint(VL0);
6424       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6425       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6426       V = ShuffleBuilder.finalize(V);
6427       E->VectorizedValue = V;
6428       return V;
6429     }
6430     case Instruction::ExtractValue: {
6431       auto *LI = cast<LoadInst>(E->getSingleOperand(0));
6432       Builder.SetInsertPoint(LI);
6433       auto *PtrTy = PointerType::get(VecTy, LI->getPointerAddressSpace());
6434       Value *Ptr = Builder.CreateBitCast(LI->getOperand(0), PtrTy);
6435       LoadInst *V = Builder.CreateAlignedLoad(VecTy, Ptr, LI->getAlign());
6436       Value *NewV = propagateMetadata(V, E->Scalars);
6437       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6438       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6439       NewV = ShuffleBuilder.finalize(NewV);
6440       E->VectorizedValue = NewV;
6441       return NewV;
6442     }
6443     case Instruction::InsertElement: {
6444       assert(E->ReuseShuffleIndices.empty() && "All inserts should be unique");
6445       Builder.SetInsertPoint(cast<Instruction>(E->Scalars.back()));
6446       Value *V = vectorizeTree(E->getOperand(1));
6447 
6448       // Create InsertVector shuffle if necessary
6449       auto *FirstInsert = cast<Instruction>(*find_if(E->Scalars, [E](Value *V) {
6450         return !is_contained(E->Scalars, cast<Instruction>(V)->getOperand(0));
6451       }));
6452       const unsigned NumElts =
6453           cast<FixedVectorType>(FirstInsert->getType())->getNumElements();
6454       const unsigned NumScalars = E->Scalars.size();
6455 
6456       unsigned Offset = *getInsertIndex(VL0, 0);
6457       assert(Offset < NumElts && "Failed to find vector index offset");
6458 
6459       // Create shuffle to resize vector
6460       SmallVector<int> Mask;
6461       if (!E->ReorderIndices.empty()) {
6462         inversePermutation(E->ReorderIndices, Mask);
6463         Mask.append(NumElts - NumScalars, UndefMaskElem);
6464       } else {
6465         Mask.assign(NumElts, UndefMaskElem);
6466         std::iota(Mask.begin(), std::next(Mask.begin(), NumScalars), 0);
6467       }
6468       // Create InsertVector shuffle if necessary
6469       bool IsIdentity = true;
6470       SmallVector<int> PrevMask(NumElts, UndefMaskElem);
6471       Mask.swap(PrevMask);
6472       for (unsigned I = 0; I < NumScalars; ++I) {
6473         Value *Scalar = E->Scalars[PrevMask[I]];
6474         Optional<int> InsertIdx = getInsertIndex(Scalar, 0);
6475         if (!InsertIdx || *InsertIdx == UndefMaskElem)
6476           continue;
6477         IsIdentity &= *InsertIdx - Offset == I;
6478         Mask[*InsertIdx - Offset] = I;
6479       }
6480       if (!IsIdentity || NumElts != NumScalars) {
6481         V = Builder.CreateShuffleVector(V, Mask);
6482         if (auto *I = dyn_cast<Instruction>(V)) {
6483           GatherShuffleSeq.insert(I);
6484           CSEBlocks.insert(I->getParent());
6485         }
6486       }
6487 
6488       if ((!IsIdentity || Offset != 0 ||
6489            !isUndefVector(FirstInsert->getOperand(0))) &&
6490           NumElts != NumScalars) {
6491         SmallVector<int> InsertMask(NumElts);
6492         std::iota(InsertMask.begin(), InsertMask.end(), 0);
6493         for (unsigned I = 0; I < NumElts; I++) {
6494           if (Mask[I] != UndefMaskElem)
6495             InsertMask[Offset + I] = NumElts + I;
6496         }
6497 
6498         V = Builder.CreateShuffleVector(
6499             FirstInsert->getOperand(0), V, InsertMask,
6500             cast<Instruction>(E->Scalars.back())->getName());
6501         if (auto *I = dyn_cast<Instruction>(V)) {
6502           GatherShuffleSeq.insert(I);
6503           CSEBlocks.insert(I->getParent());
6504         }
6505       }
6506 
6507       ++NumVectorInstructions;
6508       E->VectorizedValue = V;
6509       return V;
6510     }
6511     case Instruction::ZExt:
6512     case Instruction::SExt:
6513     case Instruction::FPToUI:
6514     case Instruction::FPToSI:
6515     case Instruction::FPExt:
6516     case Instruction::PtrToInt:
6517     case Instruction::IntToPtr:
6518     case Instruction::SIToFP:
6519     case Instruction::UIToFP:
6520     case Instruction::Trunc:
6521     case Instruction::FPTrunc:
6522     case Instruction::BitCast: {
6523       setInsertPointAfterBundle(E);
6524 
6525       Value *InVec = vectorizeTree(E->getOperand(0));
6526 
6527       if (E->VectorizedValue) {
6528         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6529         return E->VectorizedValue;
6530       }
6531 
6532       auto *CI = cast<CastInst>(VL0);
6533       Value *V = Builder.CreateCast(CI->getOpcode(), InVec, VecTy);
6534       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6535       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6536       V = ShuffleBuilder.finalize(V);
6537 
6538       E->VectorizedValue = V;
6539       ++NumVectorInstructions;
6540       return V;
6541     }
6542     case Instruction::FCmp:
6543     case Instruction::ICmp: {
6544       setInsertPointAfterBundle(E);
6545 
6546       Value *L = vectorizeTree(E->getOperand(0));
6547       Value *R = vectorizeTree(E->getOperand(1));
6548 
6549       if (E->VectorizedValue) {
6550         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6551         return E->VectorizedValue;
6552       }
6553 
6554       CmpInst::Predicate P0 = cast<CmpInst>(VL0)->getPredicate();
6555       Value *V = Builder.CreateCmp(P0, L, R);
6556       propagateIRFlags(V, E->Scalars, VL0);
6557       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6558       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6559       V = ShuffleBuilder.finalize(V);
6560 
6561       E->VectorizedValue = V;
6562       ++NumVectorInstructions;
6563       return V;
6564     }
6565     case Instruction::Select: {
6566       setInsertPointAfterBundle(E);
6567 
6568       Value *Cond = vectorizeTree(E->getOperand(0));
6569       Value *True = vectorizeTree(E->getOperand(1));
6570       Value *False = vectorizeTree(E->getOperand(2));
6571 
6572       if (E->VectorizedValue) {
6573         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6574         return E->VectorizedValue;
6575       }
6576 
6577       Value *V = Builder.CreateSelect(Cond, True, False);
6578       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6579       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6580       V = ShuffleBuilder.finalize(V);
6581 
6582       E->VectorizedValue = V;
6583       ++NumVectorInstructions;
6584       return V;
6585     }
6586     case Instruction::FNeg: {
6587       setInsertPointAfterBundle(E);
6588 
6589       Value *Op = vectorizeTree(E->getOperand(0));
6590 
6591       if (E->VectorizedValue) {
6592         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6593         return E->VectorizedValue;
6594       }
6595 
6596       Value *V = Builder.CreateUnOp(
6597           static_cast<Instruction::UnaryOps>(E->getOpcode()), Op);
6598       propagateIRFlags(V, E->Scalars, VL0);
6599       if (auto *I = dyn_cast<Instruction>(V))
6600         V = propagateMetadata(I, E->Scalars);
6601 
6602       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6603       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6604       V = ShuffleBuilder.finalize(V);
6605 
6606       E->VectorizedValue = V;
6607       ++NumVectorInstructions;
6608 
6609       return V;
6610     }
6611     case Instruction::Add:
6612     case Instruction::FAdd:
6613     case Instruction::Sub:
6614     case Instruction::FSub:
6615     case Instruction::Mul:
6616     case Instruction::FMul:
6617     case Instruction::UDiv:
6618     case Instruction::SDiv:
6619     case Instruction::FDiv:
6620     case Instruction::URem:
6621     case Instruction::SRem:
6622     case Instruction::FRem:
6623     case Instruction::Shl:
6624     case Instruction::LShr:
6625     case Instruction::AShr:
6626     case Instruction::And:
6627     case Instruction::Or:
6628     case Instruction::Xor: {
6629       setInsertPointAfterBundle(E);
6630 
6631       Value *LHS = vectorizeTree(E->getOperand(0));
6632       Value *RHS = vectorizeTree(E->getOperand(1));
6633 
6634       if (E->VectorizedValue) {
6635         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6636         return E->VectorizedValue;
6637       }
6638 
6639       Value *V = Builder.CreateBinOp(
6640           static_cast<Instruction::BinaryOps>(E->getOpcode()), LHS,
6641           RHS);
6642       propagateIRFlags(V, E->Scalars, VL0);
6643       if (auto *I = dyn_cast<Instruction>(V))
6644         V = propagateMetadata(I, E->Scalars);
6645 
6646       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6647       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6648       V = ShuffleBuilder.finalize(V);
6649 
6650       E->VectorizedValue = V;
6651       ++NumVectorInstructions;
6652 
6653       return V;
6654     }
6655     case Instruction::Load: {
6656       // Loads are inserted at the head of the tree because we don't want to
6657       // sink them all the way down past store instructions.
6658       setInsertPointAfterBundle(E);
6659 
6660       LoadInst *LI = cast<LoadInst>(VL0);
6661       Instruction *NewLI;
6662       unsigned AS = LI->getPointerAddressSpace();
6663       Value *PO = LI->getPointerOperand();
6664       if (E->State == TreeEntry::Vectorize) {
6665 
6666         Value *VecPtr = Builder.CreateBitCast(PO, VecTy->getPointerTo(AS));
6667 
6668         // The pointer operand uses an in-tree scalar so we add the new BitCast
6669         // to ExternalUses list to make sure that an extract will be generated
6670         // in the future.
6671         if (TreeEntry *Entry = getTreeEntry(PO)) {
6672           // Find which lane we need to extract.
6673           unsigned FoundLane = Entry->findLaneForValue(PO);
6674           ExternalUses.emplace_back(PO, cast<User>(VecPtr), FoundLane);
6675         }
6676 
6677         NewLI = Builder.CreateAlignedLoad(VecTy, VecPtr, LI->getAlign());
6678       } else {
6679         assert(E->State == TreeEntry::ScatterVectorize && "Unhandled state");
6680         Value *VecPtr = vectorizeTree(E->getOperand(0));
6681         // Use the minimum alignment of the gathered loads.
6682         Align CommonAlignment = LI->getAlign();
6683         for (Value *V : E->Scalars)
6684           CommonAlignment =
6685               commonAlignment(CommonAlignment, cast<LoadInst>(V)->getAlign());
6686         NewLI = Builder.CreateMaskedGather(VecTy, VecPtr, CommonAlignment);
6687       }
6688       Value *V = propagateMetadata(NewLI, E->Scalars);
6689 
6690       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6691       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6692       V = ShuffleBuilder.finalize(V);
6693       E->VectorizedValue = V;
6694       ++NumVectorInstructions;
6695       return V;
6696     }
6697     case Instruction::Store: {
6698       auto *SI = cast<StoreInst>(VL0);
6699       unsigned AS = SI->getPointerAddressSpace();
6700 
6701       setInsertPointAfterBundle(E);
6702 
6703       Value *VecValue = vectorizeTree(E->getOperand(0));
6704       ShuffleBuilder.addMask(E->ReorderIndices);
6705       VecValue = ShuffleBuilder.finalize(VecValue);
6706 
6707       Value *ScalarPtr = SI->getPointerOperand();
6708       Value *VecPtr = Builder.CreateBitCast(
6709           ScalarPtr, VecValue->getType()->getPointerTo(AS));
6710       StoreInst *ST = Builder.CreateAlignedStore(VecValue, VecPtr,
6711                                                  SI->getAlign());
6712 
6713       // The pointer operand uses an in-tree scalar, so add the new BitCast to
6714       // ExternalUses to make sure that an extract will be generated in the
6715       // future.
6716       if (TreeEntry *Entry = getTreeEntry(ScalarPtr)) {
6717         // Find which lane we need to extract.
6718         unsigned FoundLane = Entry->findLaneForValue(ScalarPtr);
6719         ExternalUses.push_back(
6720             ExternalUser(ScalarPtr, cast<User>(VecPtr), FoundLane));
6721       }
6722 
6723       Value *V = propagateMetadata(ST, E->Scalars);
6724 
6725       E->VectorizedValue = V;
6726       ++NumVectorInstructions;
6727       return V;
6728     }
6729     case Instruction::GetElementPtr: {
6730       auto *GEP0 = cast<GetElementPtrInst>(VL0);
6731       setInsertPointAfterBundle(E);
6732 
6733       Value *Op0 = vectorizeTree(E->getOperand(0));
6734 
6735       SmallVector<Value *> OpVecs;
6736       for (int J = 1, N = GEP0->getNumOperands(); J < N; ++J) {
6737         Value *OpVec = vectorizeTree(E->getOperand(J));
6738         OpVecs.push_back(OpVec);
6739       }
6740 
6741       Value *V = Builder.CreateGEP(GEP0->getSourceElementType(), Op0, OpVecs);
6742       if (Instruction *I = dyn_cast<Instruction>(V))
6743         V = propagateMetadata(I, E->Scalars);
6744 
6745       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6746       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6747       V = ShuffleBuilder.finalize(V);
6748 
6749       E->VectorizedValue = V;
6750       ++NumVectorInstructions;
6751 
6752       return V;
6753     }
6754     case Instruction::Call: {
6755       CallInst *CI = cast<CallInst>(VL0);
6756       setInsertPointAfterBundle(E);
6757 
6758       Intrinsic::ID IID  = Intrinsic::not_intrinsic;
6759       if (Function *FI = CI->getCalledFunction())
6760         IID = FI->getIntrinsicID();
6761 
6762       Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
6763 
6764       auto VecCallCosts = getVectorCallCosts(CI, VecTy, TTI, TLI);
6765       bool UseIntrinsic = ID != Intrinsic::not_intrinsic &&
6766                           VecCallCosts.first <= VecCallCosts.second;
6767 
6768       Value *ScalarArg = nullptr;
6769       std::vector<Value *> OpVecs;
6770       SmallVector<Type *, 2> TysForDecl =
6771           {FixedVectorType::get(CI->getType(), E->Scalars.size())};
6772       for (int j = 0, e = CI->arg_size(); j < e; ++j) {
6773         ValueList OpVL;
6774         // Some intrinsics have scalar arguments. This argument should not be
6775         // vectorized.
6776         if (UseIntrinsic && hasVectorInstrinsicScalarOpd(IID, j)) {
6777           CallInst *CEI = cast<CallInst>(VL0);
6778           ScalarArg = CEI->getArgOperand(j);
6779           OpVecs.push_back(CEI->getArgOperand(j));
6780           if (hasVectorInstrinsicOverloadedScalarOpd(IID, j))
6781             TysForDecl.push_back(ScalarArg->getType());
6782           continue;
6783         }
6784 
6785         Value *OpVec = vectorizeTree(E->getOperand(j));
6786         LLVM_DEBUG(dbgs() << "SLP: OpVec[" << j << "]: " << *OpVec << "\n");
6787         OpVecs.push_back(OpVec);
6788       }
6789 
6790       Function *CF;
6791       if (!UseIntrinsic) {
6792         VFShape Shape =
6793             VFShape::get(*CI, ElementCount::getFixed(static_cast<unsigned>(
6794                                   VecTy->getNumElements())),
6795                          false /*HasGlobalPred*/);
6796         CF = VFDatabase(*CI).getVectorizedFunction(Shape);
6797       } else {
6798         CF = Intrinsic::getDeclaration(F->getParent(), ID, TysForDecl);
6799       }
6800 
6801       SmallVector<OperandBundleDef, 1> OpBundles;
6802       CI->getOperandBundlesAsDefs(OpBundles);
6803       Value *V = Builder.CreateCall(CF, OpVecs, OpBundles);
6804 
6805       // The scalar argument uses an in-tree scalar so we add the new vectorized
6806       // call to ExternalUses list to make sure that an extract will be
6807       // generated in the future.
6808       if (ScalarArg) {
6809         if (TreeEntry *Entry = getTreeEntry(ScalarArg)) {
6810           // Find which lane we need to extract.
6811           unsigned FoundLane = Entry->findLaneForValue(ScalarArg);
6812           ExternalUses.push_back(
6813               ExternalUser(ScalarArg, cast<User>(V), FoundLane));
6814         }
6815       }
6816 
6817       propagateIRFlags(V, E->Scalars, VL0);
6818       ShuffleBuilder.addInversedMask(E->ReorderIndices);
6819       ShuffleBuilder.addMask(E->ReuseShuffleIndices);
6820       V = ShuffleBuilder.finalize(V);
6821 
6822       E->VectorizedValue = V;
6823       ++NumVectorInstructions;
6824       return V;
6825     }
6826     case Instruction::ShuffleVector: {
6827       assert(E->isAltShuffle() &&
6828              ((Instruction::isBinaryOp(E->getOpcode()) &&
6829                Instruction::isBinaryOp(E->getAltOpcode())) ||
6830               (Instruction::isCast(E->getOpcode()) &&
6831                Instruction::isCast(E->getAltOpcode()))) &&
6832              "Invalid Shuffle Vector Operand");
6833 
6834       Value *LHS = nullptr, *RHS = nullptr;
6835       if (Instruction::isBinaryOp(E->getOpcode())) {
6836         setInsertPointAfterBundle(E);
6837         LHS = vectorizeTree(E->getOperand(0));
6838         RHS = vectorizeTree(E->getOperand(1));
6839       } else {
6840         setInsertPointAfterBundle(E);
6841         LHS = vectorizeTree(E->getOperand(0));
6842       }
6843 
6844       if (E->VectorizedValue) {
6845         LLVM_DEBUG(dbgs() << "SLP: Diamond merged for " << *VL0 << ".\n");
6846         return E->VectorizedValue;
6847       }
6848 
6849       Value *V0, *V1;
6850       if (Instruction::isBinaryOp(E->getOpcode())) {
6851         V0 = Builder.CreateBinOp(
6852             static_cast<Instruction::BinaryOps>(E->getOpcode()), LHS, RHS);
6853         V1 = Builder.CreateBinOp(
6854             static_cast<Instruction::BinaryOps>(E->getAltOpcode()), LHS, RHS);
6855       } else {
6856         V0 = Builder.CreateCast(
6857             static_cast<Instruction::CastOps>(E->getOpcode()), LHS, VecTy);
6858         V1 = Builder.CreateCast(
6859             static_cast<Instruction::CastOps>(E->getAltOpcode()), LHS, VecTy);
6860       }
6861       // Add V0 and V1 to later analysis to try to find and remove matching
6862       // instruction, if any.
6863       for (Value *V : {V0, V1}) {
6864         if (auto *I = dyn_cast<Instruction>(V)) {
6865           GatherShuffleSeq.insert(I);
6866           CSEBlocks.insert(I->getParent());
6867         }
6868       }
6869 
6870       // Create shuffle to take alternate operations from the vector.
6871       // Also, gather up main and alt scalar ops to propagate IR flags to
6872       // each vector operation.
6873       ValueList OpScalars, AltScalars;
6874       SmallVector<int> Mask;
6875       buildSuffleEntryMask(
6876           E->Scalars, E->ReorderIndices, E->ReuseShuffleIndices,
6877           [E](Instruction *I) {
6878             assert(E->isOpcodeOrAlt(I) && "Unexpected main/alternate opcode");
6879             return I->getOpcode() == E->getAltOpcode();
6880           },
6881           Mask, &OpScalars, &AltScalars);
6882 
6883       propagateIRFlags(V0, OpScalars);
6884       propagateIRFlags(V1, AltScalars);
6885 
6886       Value *V = Builder.CreateShuffleVector(V0, V1, Mask);
6887       if (auto *I = dyn_cast<Instruction>(V)) {
6888         V = propagateMetadata(I, E->Scalars);
6889         GatherShuffleSeq.insert(I);
6890         CSEBlocks.insert(I->getParent());
6891       }
6892       V = ShuffleBuilder.finalize(V);
6893 
6894       E->VectorizedValue = V;
6895       ++NumVectorInstructions;
6896 
6897       return V;
6898     }
6899     default:
6900     llvm_unreachable("unknown inst");
6901   }
6902   return nullptr;
6903 }
6904 
6905 Value *BoUpSLP::vectorizeTree() {
6906   ExtraValueToDebugLocsMap ExternallyUsedValues;
6907   return vectorizeTree(ExternallyUsedValues);
6908 }
6909 
6910 Value *
6911 BoUpSLP::vectorizeTree(ExtraValueToDebugLocsMap &ExternallyUsedValues) {
6912   // All blocks must be scheduled before any instructions are inserted.
6913   for (auto &BSIter : BlocksSchedules) {
6914     scheduleBlock(BSIter.second.get());
6915   }
6916 
6917   Builder.SetInsertPoint(&F->getEntryBlock().front());
6918   auto *VectorRoot = vectorizeTree(VectorizableTree[0].get());
6919 
6920   // If the vectorized tree can be rewritten in a smaller type, we truncate the
6921   // vectorized root. InstCombine will then rewrite the entire expression. We
6922   // sign extend the extracted values below.
6923   auto *ScalarRoot = VectorizableTree[0]->Scalars[0];
6924   if (MinBWs.count(ScalarRoot)) {
6925     if (auto *I = dyn_cast<Instruction>(VectorRoot)) {
6926       // If current instr is a phi and not the last phi, insert it after the
6927       // last phi node.
6928       if (isa<PHINode>(I))
6929         Builder.SetInsertPoint(&*I->getParent()->getFirstInsertionPt());
6930       else
6931         Builder.SetInsertPoint(&*++BasicBlock::iterator(I));
6932     }
6933     auto BundleWidth = VectorizableTree[0]->Scalars.size();
6934     auto *MinTy = IntegerType::get(F->getContext(), MinBWs[ScalarRoot].first);
6935     auto *VecTy = FixedVectorType::get(MinTy, BundleWidth);
6936     auto *Trunc = Builder.CreateTrunc(VectorRoot, VecTy);
6937     VectorizableTree[0]->VectorizedValue = Trunc;
6938   }
6939 
6940   LLVM_DEBUG(dbgs() << "SLP: Extracting " << ExternalUses.size()
6941                     << " values .\n");
6942 
6943   // Extract all of the elements with the external uses.
6944   for (const auto &ExternalUse : ExternalUses) {
6945     Value *Scalar = ExternalUse.Scalar;
6946     llvm::User *User = ExternalUse.User;
6947 
6948     // Skip users that we already RAUW. This happens when one instruction
6949     // has multiple uses of the same value.
6950     if (User && !is_contained(Scalar->users(), User))
6951       continue;
6952     TreeEntry *E = getTreeEntry(Scalar);
6953     assert(E && "Invalid scalar");
6954     assert(E->State != TreeEntry::NeedToGather &&
6955            "Extracting from a gather list");
6956 
6957     Value *Vec = E->VectorizedValue;
6958     assert(Vec && "Can't find vectorizable value");
6959 
6960     Value *Lane = Builder.getInt32(ExternalUse.Lane);
6961     auto ExtractAndExtendIfNeeded = [&](Value *Vec) {
6962       if (Scalar->getType() != Vec->getType()) {
6963         Value *Ex;
6964         // "Reuse" the existing extract to improve final codegen.
6965         if (auto *ES = dyn_cast<ExtractElementInst>(Scalar)) {
6966           Ex = Builder.CreateExtractElement(ES->getOperand(0),
6967                                             ES->getOperand(1));
6968         } else {
6969           Ex = Builder.CreateExtractElement(Vec, Lane);
6970         }
6971         // If necessary, sign-extend or zero-extend ScalarRoot
6972         // to the larger type.
6973         if (!MinBWs.count(ScalarRoot))
6974           return Ex;
6975         if (MinBWs[ScalarRoot].second)
6976           return Builder.CreateSExt(Ex, Scalar->getType());
6977         return Builder.CreateZExt(Ex, Scalar->getType());
6978       }
6979       assert(isa<FixedVectorType>(Scalar->getType()) &&
6980              isa<InsertElementInst>(Scalar) &&
6981              "In-tree scalar of vector type is not insertelement?");
6982       return Vec;
6983     };
6984     // If User == nullptr, the Scalar is used as extra arg. Generate
6985     // ExtractElement instruction and update the record for this scalar in
6986     // ExternallyUsedValues.
6987     if (!User) {
6988       assert(ExternallyUsedValues.count(Scalar) &&
6989              "Scalar with nullptr as an external user must be registered in "
6990              "ExternallyUsedValues map");
6991       if (auto *VecI = dyn_cast<Instruction>(Vec)) {
6992         Builder.SetInsertPoint(VecI->getParent(),
6993                                std::next(VecI->getIterator()));
6994       } else {
6995         Builder.SetInsertPoint(&F->getEntryBlock().front());
6996       }
6997       Value *NewInst = ExtractAndExtendIfNeeded(Vec);
6998       CSEBlocks.insert(cast<Instruction>(Scalar)->getParent());
6999       auto &NewInstLocs = ExternallyUsedValues[NewInst];
7000       auto It = ExternallyUsedValues.find(Scalar);
7001       assert(It != ExternallyUsedValues.end() &&
7002              "Externally used scalar is not found in ExternallyUsedValues");
7003       NewInstLocs.append(It->second);
7004       ExternallyUsedValues.erase(Scalar);
7005       // Required to update internally referenced instructions.
7006       Scalar->replaceAllUsesWith(NewInst);
7007       continue;
7008     }
7009 
7010     // Generate extracts for out-of-tree users.
7011     // Find the insertion point for the extractelement lane.
7012     if (auto *VecI = dyn_cast<Instruction>(Vec)) {
7013       if (PHINode *PH = dyn_cast<PHINode>(User)) {
7014         for (int i = 0, e = PH->getNumIncomingValues(); i != e; ++i) {
7015           if (PH->getIncomingValue(i) == Scalar) {
7016             Instruction *IncomingTerminator =
7017                 PH->getIncomingBlock(i)->getTerminator();
7018             if (isa<CatchSwitchInst>(IncomingTerminator)) {
7019               Builder.SetInsertPoint(VecI->getParent(),
7020                                      std::next(VecI->getIterator()));
7021             } else {
7022               Builder.SetInsertPoint(PH->getIncomingBlock(i)->getTerminator());
7023             }
7024             Value *NewInst = ExtractAndExtendIfNeeded(Vec);
7025             CSEBlocks.insert(PH->getIncomingBlock(i));
7026             PH->setOperand(i, NewInst);
7027           }
7028         }
7029       } else {
7030         Builder.SetInsertPoint(cast<Instruction>(User));
7031         Value *NewInst = ExtractAndExtendIfNeeded(Vec);
7032         CSEBlocks.insert(cast<Instruction>(User)->getParent());
7033         User->replaceUsesOfWith(Scalar, NewInst);
7034       }
7035     } else {
7036       Builder.SetInsertPoint(&F->getEntryBlock().front());
7037       Value *NewInst = ExtractAndExtendIfNeeded(Vec);
7038       CSEBlocks.insert(&F->getEntryBlock());
7039       User->replaceUsesOfWith(Scalar, NewInst);
7040     }
7041 
7042     LLVM_DEBUG(dbgs() << "SLP: Replaced:" << *User << ".\n");
7043   }
7044 
7045   // For each vectorized value:
7046   for (auto &TEPtr : VectorizableTree) {
7047     TreeEntry *Entry = TEPtr.get();
7048 
7049     // No need to handle users of gathered values.
7050     if (Entry->State == TreeEntry::NeedToGather)
7051       continue;
7052 
7053     assert(Entry->VectorizedValue && "Can't find vectorizable value");
7054 
7055     // For each lane:
7056     for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
7057       Value *Scalar = Entry->Scalars[Lane];
7058 
7059 #ifndef NDEBUG
7060       Type *Ty = Scalar->getType();
7061       if (!Ty->isVoidTy()) {
7062         for (User *U : Scalar->users()) {
7063           LLVM_DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
7064 
7065           // It is legal to delete users in the ignorelist.
7066           assert((getTreeEntry(U) || is_contained(UserIgnoreList, U) ||
7067                   (isa_and_nonnull<Instruction>(U) &&
7068                    isDeleted(cast<Instruction>(U)))) &&
7069                  "Deleting out-of-tree value");
7070         }
7071       }
7072 #endif
7073       LLVM_DEBUG(dbgs() << "SLP: \tErasing scalar:" << *Scalar << ".\n");
7074       eraseInstruction(cast<Instruction>(Scalar));
7075     }
7076   }
7077 
7078   Builder.ClearInsertionPoint();
7079   InstrElementSize.clear();
7080 
7081   return VectorizableTree[0]->VectorizedValue;
7082 }
7083 
7084 void BoUpSLP::optimizeGatherSequence() {
7085   LLVM_DEBUG(dbgs() << "SLP: Optimizing " << GatherShuffleSeq.size()
7086                     << " gather sequences instructions.\n");
7087   // LICM InsertElementInst sequences.
7088   for (Instruction *I : GatherShuffleSeq) {
7089     if (isDeleted(I))
7090       continue;
7091 
7092     // Check if this block is inside a loop.
7093     Loop *L = LI->getLoopFor(I->getParent());
7094     if (!L)
7095       continue;
7096 
7097     // Check if it has a preheader.
7098     BasicBlock *PreHeader = L->getLoopPreheader();
7099     if (!PreHeader)
7100       continue;
7101 
7102     // If the vector or the element that we insert into it are
7103     // instructions that are defined in this basic block then we can't
7104     // hoist this instruction.
7105     if (any_of(I->operands(), [L](Value *V) {
7106           auto *OpI = dyn_cast<Instruction>(V);
7107           return OpI && L->contains(OpI);
7108         }))
7109       continue;
7110 
7111     // We can hoist this instruction. Move it to the pre-header.
7112     I->moveBefore(PreHeader->getTerminator());
7113   }
7114 
7115   // Make a list of all reachable blocks in our CSE queue.
7116   SmallVector<const DomTreeNode *, 8> CSEWorkList;
7117   CSEWorkList.reserve(CSEBlocks.size());
7118   for (BasicBlock *BB : CSEBlocks)
7119     if (DomTreeNode *N = DT->getNode(BB)) {
7120       assert(DT->isReachableFromEntry(N));
7121       CSEWorkList.push_back(N);
7122     }
7123 
7124   // Sort blocks by domination. This ensures we visit a block after all blocks
7125   // dominating it are visited.
7126   llvm::sort(CSEWorkList, [](const DomTreeNode *A, const DomTreeNode *B) {
7127     assert((A == B) == (A->getDFSNumIn() == B->getDFSNumIn()) &&
7128            "Different nodes should have different DFS numbers");
7129     return A->getDFSNumIn() < B->getDFSNumIn();
7130   });
7131 
7132   // Less defined shuffles can be replaced by the more defined copies.
7133   // Between two shuffles one is less defined if it has the same vector operands
7134   // and its mask indeces are the same as in the first one or undefs. E.g.
7135   // shuffle %0, poison, <0, 0, 0, undef> is less defined than shuffle %0,
7136   // poison, <0, 0, 0, 0>.
7137   auto &&IsIdenticalOrLessDefined = [this](Instruction *I1, Instruction *I2,
7138                                            SmallVectorImpl<int> &NewMask) {
7139     if (I1->getType() != I2->getType())
7140       return false;
7141     auto *SI1 = dyn_cast<ShuffleVectorInst>(I1);
7142     auto *SI2 = dyn_cast<ShuffleVectorInst>(I2);
7143     if (!SI1 || !SI2)
7144       return I1->isIdenticalTo(I2);
7145     if (SI1->isIdenticalTo(SI2))
7146       return true;
7147     for (int I = 0, E = SI1->getNumOperands(); I < E; ++I)
7148       if (SI1->getOperand(I) != SI2->getOperand(I))
7149         return false;
7150     // Check if the second instruction is more defined than the first one.
7151     NewMask.assign(SI2->getShuffleMask().begin(), SI2->getShuffleMask().end());
7152     ArrayRef<int> SM1 = SI1->getShuffleMask();
7153     // Count trailing undefs in the mask to check the final number of used
7154     // registers.
7155     unsigned LastUndefsCnt = 0;
7156     for (int I = 0, E = NewMask.size(); I < E; ++I) {
7157       if (SM1[I] == UndefMaskElem)
7158         ++LastUndefsCnt;
7159       else
7160         LastUndefsCnt = 0;
7161       if (NewMask[I] != UndefMaskElem && SM1[I] != UndefMaskElem &&
7162           NewMask[I] != SM1[I])
7163         return false;
7164       if (NewMask[I] == UndefMaskElem)
7165         NewMask[I] = SM1[I];
7166     }
7167     // Check if the last undefs actually change the final number of used vector
7168     // registers.
7169     return SM1.size() - LastUndefsCnt > 1 &&
7170            TTI->getNumberOfParts(SI1->getType()) ==
7171                TTI->getNumberOfParts(
7172                    FixedVectorType::get(SI1->getType()->getElementType(),
7173                                         SM1.size() - LastUndefsCnt));
7174   };
7175   // Perform O(N^2) search over the gather/shuffle sequences and merge identical
7176   // instructions. TODO: We can further optimize this scan if we split the
7177   // instructions into different buckets based on the insert lane.
7178   SmallVector<Instruction *, 16> Visited;
7179   for (auto I = CSEWorkList.begin(), E = CSEWorkList.end(); I != E; ++I) {
7180     assert(*I &&
7181            (I == CSEWorkList.begin() || !DT->dominates(*I, *std::prev(I))) &&
7182            "Worklist not sorted properly!");
7183     BasicBlock *BB = (*I)->getBlock();
7184     // For all instructions in blocks containing gather sequences:
7185     for (Instruction &In : llvm::make_early_inc_range(*BB)) {
7186       if (isDeleted(&In))
7187         continue;
7188       if (!isa<InsertElementInst>(&In) && !isa<ExtractElementInst>(&In) &&
7189           !isa<ShuffleVectorInst>(&In) && !GatherShuffleSeq.contains(&In))
7190         continue;
7191 
7192       // Check if we can replace this instruction with any of the
7193       // visited instructions.
7194       bool Replaced = false;
7195       for (Instruction *&V : Visited) {
7196         SmallVector<int> NewMask;
7197         if (IsIdenticalOrLessDefined(&In, V, NewMask) &&
7198             DT->dominates(V->getParent(), In.getParent())) {
7199           In.replaceAllUsesWith(V);
7200           eraseInstruction(&In);
7201           if (auto *SI = dyn_cast<ShuffleVectorInst>(V))
7202             if (!NewMask.empty())
7203               SI->setShuffleMask(NewMask);
7204           Replaced = true;
7205           break;
7206         }
7207         if (isa<ShuffleVectorInst>(In) && isa<ShuffleVectorInst>(V) &&
7208             GatherShuffleSeq.contains(V) &&
7209             IsIdenticalOrLessDefined(V, &In, NewMask) &&
7210             DT->dominates(In.getParent(), V->getParent())) {
7211           In.moveAfter(V);
7212           V->replaceAllUsesWith(&In);
7213           eraseInstruction(V);
7214           if (auto *SI = dyn_cast<ShuffleVectorInst>(&In))
7215             if (!NewMask.empty())
7216               SI->setShuffleMask(NewMask);
7217           V = &In;
7218           Replaced = true;
7219           break;
7220         }
7221       }
7222       if (!Replaced) {
7223         assert(!is_contained(Visited, &In));
7224         Visited.push_back(&In);
7225       }
7226     }
7227   }
7228   CSEBlocks.clear();
7229   GatherShuffleSeq.clear();
7230 }
7231 
7232 // Groups the instructions to a bundle (which is then a single scheduling entity)
7233 // and schedules instructions until the bundle gets ready.
7234 Optional<BoUpSLP::ScheduleData *>
7235 BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL, BoUpSLP *SLP,
7236                                             const InstructionsState &S) {
7237   // No need to schedule PHIs, insertelement, extractelement and extractvalue
7238   // instructions.
7239   if (isa<PHINode>(S.OpValue) || isVectorLikeInstWithConstOps(S.OpValue))
7240     return nullptr;
7241 
7242   // Initialize the instruction bundle.
7243   Instruction *OldScheduleEnd = ScheduleEnd;
7244   ScheduleData *PrevInBundle = nullptr;
7245   ScheduleData *Bundle = nullptr;
7246   bool ReSchedule = false;
7247   LLVM_DEBUG(dbgs() << "SLP:  bundle: " << *S.OpValue << "\n");
7248 
7249   auto &&TryScheduleBundle = [this, OldScheduleEnd, SLP](bool ReSchedule,
7250                                                          ScheduleData *Bundle) {
7251     // The scheduling region got new instructions at the lower end (or it is a
7252     // new region for the first bundle). This makes it necessary to
7253     // recalculate all dependencies.
7254     // It is seldom that this needs to be done a second time after adding the
7255     // initial bundle to the region.
7256     if (ScheduleEnd != OldScheduleEnd) {
7257       for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode())
7258         doForAllOpcodes(I, [](ScheduleData *SD) { SD->clearDependencies(); });
7259       ReSchedule = true;
7260     }
7261     if (ReSchedule) {
7262       resetSchedule();
7263       initialFillReadyList(ReadyInsts);
7264     }
7265     if (Bundle) {
7266       LLVM_DEBUG(dbgs() << "SLP: try schedule bundle " << *Bundle
7267                         << " in block " << BB->getName() << "\n");
7268       calculateDependencies(Bundle, /*InsertInReadyList=*/true, SLP);
7269     }
7270 
7271     // Now try to schedule the new bundle or (if no bundle) just calculate
7272     // dependencies. As soon as the bundle is "ready" it means that there are no
7273     // cyclic dependencies and we can schedule it. Note that's important that we
7274     // don't "schedule" the bundle yet (see cancelScheduling).
7275     while (((!Bundle && ReSchedule) || (Bundle && !Bundle->isReady())) &&
7276            !ReadyInsts.empty()) {
7277       ScheduleData *Picked = ReadyInsts.pop_back_val();
7278       if (Picked->isSchedulingEntity() && Picked->isReady())
7279         schedule(Picked, ReadyInsts);
7280     }
7281   };
7282 
7283   // Make sure that the scheduling region contains all
7284   // instructions of the bundle.
7285   for (Value *V : VL) {
7286     if (!extendSchedulingRegion(V, S)) {
7287       // If the scheduling region got new instructions at the lower end (or it
7288       // is a new region for the first bundle). This makes it necessary to
7289       // recalculate all dependencies.
7290       // Otherwise the compiler may crash trying to incorrectly calculate
7291       // dependencies and emit instruction in the wrong order at the actual
7292       // scheduling.
7293       TryScheduleBundle(/*ReSchedule=*/false, nullptr);
7294       return None;
7295     }
7296   }
7297 
7298   for (Value *V : VL) {
7299     ScheduleData *BundleMember = getScheduleData(V);
7300     assert(BundleMember &&
7301            "no ScheduleData for bundle member (maybe not in same basic block)");
7302     if (BundleMember->IsScheduled) {
7303       // A bundle member was scheduled as single instruction before and now
7304       // needs to be scheduled as part of the bundle. We just get rid of the
7305       // existing schedule.
7306       LLVM_DEBUG(dbgs() << "SLP:  reset schedule because " << *BundleMember
7307                         << " was already scheduled\n");
7308       ReSchedule = true;
7309     }
7310     assert(BundleMember->isSchedulingEntity() &&
7311            "bundle member already part of other bundle");
7312     if (PrevInBundle) {
7313       PrevInBundle->NextInBundle = BundleMember;
7314     } else {
7315       Bundle = BundleMember;
7316     }
7317     BundleMember->UnscheduledDepsInBundle = 0;
7318     Bundle->UnscheduledDepsInBundle += BundleMember->UnscheduledDeps;
7319 
7320     // Group the instructions to a bundle.
7321     BundleMember->FirstInBundle = Bundle;
7322     PrevInBundle = BundleMember;
7323   }
7324   assert(Bundle && "Failed to find schedule bundle");
7325   TryScheduleBundle(ReSchedule, Bundle);
7326   if (!Bundle->isReady()) {
7327     cancelScheduling(VL, S.OpValue);
7328     return None;
7329   }
7330   return Bundle;
7331 }
7332 
7333 void BoUpSLP::BlockScheduling::cancelScheduling(ArrayRef<Value *> VL,
7334                                                 Value *OpValue) {
7335   if (isa<PHINode>(OpValue) || isVectorLikeInstWithConstOps(OpValue))
7336     return;
7337 
7338   ScheduleData *Bundle = getScheduleData(OpValue);
7339   LLVM_DEBUG(dbgs() << "SLP:  cancel scheduling of " << *Bundle << "\n");
7340   assert(!Bundle->IsScheduled &&
7341          "Can't cancel bundle which is already scheduled");
7342   assert(Bundle->isSchedulingEntity() && Bundle->isPartOfBundle() &&
7343          "tried to unbundle something which is not a bundle");
7344 
7345   // Un-bundle: make single instructions out of the bundle.
7346   ScheduleData *BundleMember = Bundle;
7347   while (BundleMember) {
7348     assert(BundleMember->FirstInBundle == Bundle && "corrupt bundle links");
7349     BundleMember->FirstInBundle = BundleMember;
7350     ScheduleData *Next = BundleMember->NextInBundle;
7351     BundleMember->NextInBundle = nullptr;
7352     BundleMember->UnscheduledDepsInBundle = BundleMember->UnscheduledDeps;
7353     if (BundleMember->UnscheduledDepsInBundle == 0) {
7354       ReadyInsts.insert(BundleMember);
7355     }
7356     BundleMember = Next;
7357   }
7358 }
7359 
7360 BoUpSLP::ScheduleData *BoUpSLP::BlockScheduling::allocateScheduleDataChunks() {
7361   // Allocate a new ScheduleData for the instruction.
7362   if (ChunkPos >= ChunkSize) {
7363     ScheduleDataChunks.push_back(std::make_unique<ScheduleData[]>(ChunkSize));
7364     ChunkPos = 0;
7365   }
7366   return &(ScheduleDataChunks.back()[ChunkPos++]);
7367 }
7368 
7369 bool BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V,
7370                                                       const InstructionsState &S) {
7371   if (getScheduleData(V, isOneOf(S, V)))
7372     return true;
7373   Instruction *I = dyn_cast<Instruction>(V);
7374   assert(I && "bundle member must be an instruction");
7375   assert(!isa<PHINode>(I) && !isVectorLikeInstWithConstOps(I) &&
7376          "phi nodes/insertelements/extractelements/extractvalues don't need to "
7377          "be scheduled");
7378   auto &&CheckSheduleForI = [this, &S](Instruction *I) -> bool {
7379     ScheduleData *ISD = getScheduleData(I);
7380     if (!ISD)
7381       return false;
7382     assert(isInSchedulingRegion(ISD) &&
7383            "ScheduleData not in scheduling region");
7384     ScheduleData *SD = allocateScheduleDataChunks();
7385     SD->Inst = I;
7386     SD->init(SchedulingRegionID, S.OpValue);
7387     ExtraScheduleDataMap[I][S.OpValue] = SD;
7388     return true;
7389   };
7390   if (CheckSheduleForI(I))
7391     return true;
7392   if (!ScheduleStart) {
7393     // It's the first instruction in the new region.
7394     initScheduleData(I, I->getNextNode(), nullptr, nullptr);
7395     ScheduleStart = I;
7396     ScheduleEnd = I->getNextNode();
7397     if (isOneOf(S, I) != I)
7398       CheckSheduleForI(I);
7399     assert(ScheduleEnd && "tried to vectorize a terminator?");
7400     LLVM_DEBUG(dbgs() << "SLP:  initialize schedule region to " << *I << "\n");
7401     return true;
7402   }
7403   // Search up and down at the same time, because we don't know if the new
7404   // instruction is above or below the existing scheduling region.
7405   BasicBlock::reverse_iterator UpIter =
7406       ++ScheduleStart->getIterator().getReverse();
7407   BasicBlock::reverse_iterator UpperEnd = BB->rend();
7408   BasicBlock::iterator DownIter = ScheduleEnd->getIterator();
7409   BasicBlock::iterator LowerEnd = BB->end();
7410   while (UpIter != UpperEnd && DownIter != LowerEnd && &*UpIter != I &&
7411          &*DownIter != I) {
7412     if (++ScheduleRegionSize > ScheduleRegionSizeLimit) {
7413       LLVM_DEBUG(dbgs() << "SLP:  exceeded schedule region size limit\n");
7414       return false;
7415     }
7416 
7417     ++UpIter;
7418     ++DownIter;
7419   }
7420   if (DownIter == LowerEnd || (UpIter != UpperEnd && &*UpIter == I)) {
7421     assert(I->getParent() == ScheduleStart->getParent() &&
7422            "Instruction is in wrong basic block.");
7423     initScheduleData(I, ScheduleStart, nullptr, FirstLoadStoreInRegion);
7424     ScheduleStart = I;
7425     if (isOneOf(S, I) != I)
7426       CheckSheduleForI(I);
7427     LLVM_DEBUG(dbgs() << "SLP:  extend schedule region start to " << *I
7428                       << "\n");
7429     return true;
7430   }
7431   assert((UpIter == UpperEnd || (DownIter != LowerEnd && &*DownIter == I)) &&
7432          "Expected to reach top of the basic block or instruction down the "
7433          "lower end.");
7434   assert(I->getParent() == ScheduleEnd->getParent() &&
7435          "Instruction is in wrong basic block.");
7436   initScheduleData(ScheduleEnd, I->getNextNode(), LastLoadStoreInRegion,
7437                    nullptr);
7438   ScheduleEnd = I->getNextNode();
7439   if (isOneOf(S, I) != I)
7440     CheckSheduleForI(I);
7441   assert(ScheduleEnd && "tried to vectorize a terminator?");
7442   LLVM_DEBUG(dbgs() << "SLP:  extend schedule region end to " << *I << "\n");
7443   return true;
7444 }
7445 
7446 void BoUpSLP::BlockScheduling::initScheduleData(Instruction *FromI,
7447                                                 Instruction *ToI,
7448                                                 ScheduleData *PrevLoadStore,
7449                                                 ScheduleData *NextLoadStore) {
7450   ScheduleData *CurrentLoadStore = PrevLoadStore;
7451   for (Instruction *I = FromI; I != ToI; I = I->getNextNode()) {
7452     ScheduleData *SD = ScheduleDataMap[I];
7453     if (!SD) {
7454       SD = allocateScheduleDataChunks();
7455       ScheduleDataMap[I] = SD;
7456       SD->Inst = I;
7457     }
7458     assert(!isInSchedulingRegion(SD) &&
7459            "new ScheduleData already in scheduling region");
7460     SD->init(SchedulingRegionID, I);
7461 
7462     if (I->mayReadOrWriteMemory() &&
7463         (!isa<IntrinsicInst>(I) ||
7464          (cast<IntrinsicInst>(I)->getIntrinsicID() != Intrinsic::sideeffect &&
7465           cast<IntrinsicInst>(I)->getIntrinsicID() !=
7466               Intrinsic::pseudoprobe))) {
7467       // Update the linked list of memory accessing instructions.
7468       if (CurrentLoadStore) {
7469         CurrentLoadStore->NextLoadStore = SD;
7470       } else {
7471         FirstLoadStoreInRegion = SD;
7472       }
7473       CurrentLoadStore = SD;
7474     }
7475   }
7476   if (NextLoadStore) {
7477     if (CurrentLoadStore)
7478       CurrentLoadStore->NextLoadStore = NextLoadStore;
7479   } else {
7480     LastLoadStoreInRegion = CurrentLoadStore;
7481   }
7482 }
7483 
7484 void BoUpSLP::BlockScheduling::calculateDependencies(ScheduleData *SD,
7485                                                      bool InsertInReadyList,
7486                                                      BoUpSLP *SLP) {
7487   assert(SD->isSchedulingEntity());
7488 
7489   SmallVector<ScheduleData *, 10> WorkList;
7490   WorkList.push_back(SD);
7491 
7492   while (!WorkList.empty()) {
7493     ScheduleData *SD = WorkList.pop_back_val();
7494 
7495     ScheduleData *BundleMember = SD;
7496     while (BundleMember) {
7497       assert(isInSchedulingRegion(BundleMember));
7498       if (!BundleMember->hasValidDependencies()) {
7499 
7500         LLVM_DEBUG(dbgs() << "SLP:       update deps of " << *BundleMember
7501                           << "\n");
7502         BundleMember->Dependencies = 0;
7503         BundleMember->resetUnscheduledDeps();
7504 
7505         // Handle def-use chain dependencies.
7506         if (BundleMember->OpValue != BundleMember->Inst) {
7507           ScheduleData *UseSD = getScheduleData(BundleMember->Inst);
7508           if (UseSD && isInSchedulingRegion(UseSD->FirstInBundle)) {
7509             BundleMember->Dependencies++;
7510             ScheduleData *DestBundle = UseSD->FirstInBundle;
7511             if (!DestBundle->IsScheduled)
7512               BundleMember->incrementUnscheduledDeps(1);
7513             if (!DestBundle->hasValidDependencies())
7514               WorkList.push_back(DestBundle);
7515           }
7516         } else {
7517           for (User *U : BundleMember->Inst->users()) {
7518             if (isa<Instruction>(U)) {
7519               ScheduleData *UseSD = getScheduleData(U);
7520               if (UseSD && isInSchedulingRegion(UseSD->FirstInBundle)) {
7521                 BundleMember->Dependencies++;
7522                 ScheduleData *DestBundle = UseSD->FirstInBundle;
7523                 if (!DestBundle->IsScheduled)
7524                   BundleMember->incrementUnscheduledDeps(1);
7525                 if (!DestBundle->hasValidDependencies())
7526                   WorkList.push_back(DestBundle);
7527               }
7528             } else {
7529               // I'm not sure if this can ever happen. But we need to be safe.
7530               // This lets the instruction/bundle never be scheduled and
7531               // eventually disable vectorization.
7532               BundleMember->Dependencies++;
7533               BundleMember->incrementUnscheduledDeps(1);
7534             }
7535           }
7536         }
7537 
7538         // Handle the memory dependencies.
7539         ScheduleData *DepDest = BundleMember->NextLoadStore;
7540         if (DepDest) {
7541           Instruction *SrcInst = BundleMember->Inst;
7542           MemoryLocation SrcLoc = getLocation(SrcInst, SLP->AA);
7543           bool SrcMayWrite = BundleMember->Inst->mayWriteToMemory();
7544           unsigned numAliased = 0;
7545           unsigned DistToSrc = 1;
7546 
7547           while (DepDest) {
7548             assert(isInSchedulingRegion(DepDest));
7549 
7550             // We have two limits to reduce the complexity:
7551             // 1) AliasedCheckLimit: It's a small limit to reduce calls to
7552             //    SLP->isAliased (which is the expensive part in this loop).
7553             // 2) MaxMemDepDistance: It's for very large blocks and it aborts
7554             //    the whole loop (even if the loop is fast, it's quadratic).
7555             //    It's important for the loop break condition (see below) to
7556             //    check this limit even between two read-only instructions.
7557             if (DistToSrc >= MaxMemDepDistance ||
7558                     ((SrcMayWrite || DepDest->Inst->mayWriteToMemory()) &&
7559                      (numAliased >= AliasedCheckLimit ||
7560                       SLP->isAliased(SrcLoc, SrcInst, DepDest->Inst)))) {
7561 
7562               // We increment the counter only if the locations are aliased
7563               // (instead of counting all alias checks). This gives a better
7564               // balance between reduced runtime and accurate dependencies.
7565               numAliased++;
7566 
7567               DepDest->MemoryDependencies.push_back(BundleMember);
7568               BundleMember->Dependencies++;
7569               ScheduleData *DestBundle = DepDest->FirstInBundle;
7570               if (!DestBundle->IsScheduled) {
7571                 BundleMember->incrementUnscheduledDeps(1);
7572               }
7573               if (!DestBundle->hasValidDependencies()) {
7574                 WorkList.push_back(DestBundle);
7575               }
7576             }
7577             DepDest = DepDest->NextLoadStore;
7578 
7579             // Example, explaining the loop break condition: Let's assume our
7580             // starting instruction is i0 and MaxMemDepDistance = 3.
7581             //
7582             //                      +--------v--v--v
7583             //             i0,i1,i2,i3,i4,i5,i6,i7,i8
7584             //             +--------^--^--^
7585             //
7586             // MaxMemDepDistance let us stop alias-checking at i3 and we add
7587             // dependencies from i0 to i3,i4,.. (even if they are not aliased).
7588             // Previously we already added dependencies from i3 to i6,i7,i8
7589             // (because of MaxMemDepDistance). As we added a dependency from
7590             // i0 to i3, we have transitive dependencies from i0 to i6,i7,i8
7591             // and we can abort this loop at i6.
7592             if (DistToSrc >= 2 * MaxMemDepDistance)
7593               break;
7594             DistToSrc++;
7595           }
7596         }
7597       }
7598       BundleMember = BundleMember->NextInBundle;
7599     }
7600     if (InsertInReadyList && SD->isReady()) {
7601       ReadyInsts.push_back(SD);
7602       LLVM_DEBUG(dbgs() << "SLP:     gets ready on update: " << *SD->Inst
7603                         << "\n");
7604     }
7605   }
7606 }
7607 
7608 void BoUpSLP::BlockScheduling::resetSchedule() {
7609   assert(ScheduleStart &&
7610          "tried to reset schedule on block which has not been scheduled");
7611   for (Instruction *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
7612     doForAllOpcodes(I, [&](ScheduleData *SD) {
7613       assert(isInSchedulingRegion(SD) &&
7614              "ScheduleData not in scheduling region");
7615       SD->IsScheduled = false;
7616       SD->resetUnscheduledDeps();
7617     });
7618   }
7619   ReadyInsts.clear();
7620 }
7621 
7622 void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
7623   if (!BS->ScheduleStart)
7624     return;
7625 
7626   LLVM_DEBUG(dbgs() << "SLP: schedule block " << BS->BB->getName() << "\n");
7627 
7628   BS->resetSchedule();
7629 
7630   // For the real scheduling we use a more sophisticated ready-list: it is
7631   // sorted by the original instruction location. This lets the final schedule
7632   // be as  close as possible to the original instruction order.
7633   struct ScheduleDataCompare {
7634     bool operator()(ScheduleData *SD1, ScheduleData *SD2) const {
7635       return SD2->SchedulingPriority < SD1->SchedulingPriority;
7636     }
7637   };
7638   std::set<ScheduleData *, ScheduleDataCompare> ReadyInsts;
7639 
7640   // Ensure that all dependency data is updated and fill the ready-list with
7641   // initial instructions.
7642   int Idx = 0;
7643   int NumToSchedule = 0;
7644   for (auto *I = BS->ScheduleStart; I != BS->ScheduleEnd;
7645        I = I->getNextNode()) {
7646     BS->doForAllOpcodes(I, [this, &Idx, &NumToSchedule, BS](ScheduleData *SD) {
7647       assert((isVectorLikeInstWithConstOps(SD->Inst) ||
7648               SD->isPartOfBundle() == (getTreeEntry(SD->Inst) != nullptr)) &&
7649              "scheduler and vectorizer bundle mismatch");
7650       SD->FirstInBundle->SchedulingPriority = Idx++;
7651       if (SD->isSchedulingEntity()) {
7652         BS->calculateDependencies(SD, false, this);
7653         NumToSchedule++;
7654       }
7655     });
7656   }
7657   BS->initialFillReadyList(ReadyInsts);
7658 
7659   Instruction *LastScheduledInst = BS->ScheduleEnd;
7660 
7661   // Do the "real" scheduling.
7662   while (!ReadyInsts.empty()) {
7663     ScheduleData *picked = *ReadyInsts.begin();
7664     ReadyInsts.erase(ReadyInsts.begin());
7665 
7666     // Move the scheduled instruction(s) to their dedicated places, if not
7667     // there yet.
7668     ScheduleData *BundleMember = picked;
7669     while (BundleMember) {
7670       Instruction *pickedInst = BundleMember->Inst;
7671       if (pickedInst->getNextNode() != LastScheduledInst) {
7672         BS->BB->getInstList().remove(pickedInst);
7673         BS->BB->getInstList().insert(LastScheduledInst->getIterator(),
7674                                      pickedInst);
7675       }
7676       LastScheduledInst = pickedInst;
7677       BundleMember = BundleMember->NextInBundle;
7678     }
7679 
7680     BS->schedule(picked, ReadyInsts);
7681     NumToSchedule--;
7682   }
7683   assert(NumToSchedule == 0 && "could not schedule all instructions");
7684 
7685   // Avoid duplicate scheduling of the block.
7686   BS->ScheduleStart = nullptr;
7687 }
7688 
7689 unsigned BoUpSLP::getVectorElementSize(Value *V) {
7690   // If V is a store, just return the width of the stored value (or value
7691   // truncated just before storing) without traversing the expression tree.
7692   // This is the common case.
7693   if (auto *Store = dyn_cast<StoreInst>(V)) {
7694     if (auto *Trunc = dyn_cast<TruncInst>(Store->getValueOperand()))
7695       return DL->getTypeSizeInBits(Trunc->getSrcTy());
7696     return DL->getTypeSizeInBits(Store->getValueOperand()->getType());
7697   }
7698 
7699   if (auto *IEI = dyn_cast<InsertElementInst>(V))
7700     return getVectorElementSize(IEI->getOperand(1));
7701 
7702   auto E = InstrElementSize.find(V);
7703   if (E != InstrElementSize.end())
7704     return E->second;
7705 
7706   // If V is not a store, we can traverse the expression tree to find loads
7707   // that feed it. The type of the loaded value may indicate a more suitable
7708   // width than V's type. We want to base the vector element size on the width
7709   // of memory operations where possible.
7710   SmallVector<std::pair<Instruction *, BasicBlock *>, 16> Worklist;
7711   SmallPtrSet<Instruction *, 16> Visited;
7712   if (auto *I = dyn_cast<Instruction>(V)) {
7713     Worklist.emplace_back(I, I->getParent());
7714     Visited.insert(I);
7715   }
7716 
7717   // Traverse the expression tree in bottom-up order looking for loads. If we
7718   // encounter an instruction we don't yet handle, we give up.
7719   auto Width = 0u;
7720   while (!Worklist.empty()) {
7721     Instruction *I;
7722     BasicBlock *Parent;
7723     std::tie(I, Parent) = Worklist.pop_back_val();
7724 
7725     // We should only be looking at scalar instructions here. If the current
7726     // instruction has a vector type, skip.
7727     auto *Ty = I->getType();
7728     if (isa<VectorType>(Ty))
7729       continue;
7730 
7731     // If the current instruction is a load, update MaxWidth to reflect the
7732     // width of the loaded value.
7733     if (isa<LoadInst>(I) || isa<ExtractElementInst>(I) ||
7734         isa<ExtractValueInst>(I))
7735       Width = std::max<unsigned>(Width, DL->getTypeSizeInBits(Ty));
7736 
7737     // Otherwise, we need to visit the operands of the instruction. We only
7738     // handle the interesting cases from buildTree here. If an operand is an
7739     // instruction we haven't yet visited and from the same basic block as the
7740     // user or the use is a PHI node, we add it to the worklist.
7741     else if (isa<PHINode>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
7742              isa<CmpInst>(I) || isa<SelectInst>(I) || isa<BinaryOperator>(I) ||
7743              isa<UnaryOperator>(I)) {
7744       for (Use &U : I->operands())
7745         if (auto *J = dyn_cast<Instruction>(U.get()))
7746           if (Visited.insert(J).second &&
7747               (isa<PHINode>(I) || J->getParent() == Parent))
7748             Worklist.emplace_back(J, J->getParent());
7749     } else {
7750       break;
7751     }
7752   }
7753 
7754   // If we didn't encounter a memory access in the expression tree, or if we
7755   // gave up for some reason, just return the width of V. Otherwise, return the
7756   // maximum width we found.
7757   if (!Width) {
7758     if (auto *CI = dyn_cast<CmpInst>(V))
7759       V = CI->getOperand(0);
7760     Width = DL->getTypeSizeInBits(V->getType());
7761   }
7762 
7763   for (Instruction *I : Visited)
7764     InstrElementSize[I] = Width;
7765 
7766   return Width;
7767 }
7768 
7769 // Determine if a value V in a vectorizable expression Expr can be demoted to a
7770 // smaller type with a truncation. We collect the values that will be demoted
7771 // in ToDemote and additional roots that require investigating in Roots.
7772 static bool collectValuesToDemote(Value *V, SmallPtrSetImpl<Value *> &Expr,
7773                                   SmallVectorImpl<Value *> &ToDemote,
7774                                   SmallVectorImpl<Value *> &Roots) {
7775   // We can always demote constants.
7776   if (isa<Constant>(V)) {
7777     ToDemote.push_back(V);
7778     return true;
7779   }
7780 
7781   // If the value is not an instruction in the expression with only one use, it
7782   // cannot be demoted.
7783   auto *I = dyn_cast<Instruction>(V);
7784   if (!I || !I->hasOneUse() || !Expr.count(I))
7785     return false;
7786 
7787   switch (I->getOpcode()) {
7788 
7789   // We can always demote truncations and extensions. Since truncations can
7790   // seed additional demotion, we save the truncated value.
7791   case Instruction::Trunc:
7792     Roots.push_back(I->getOperand(0));
7793     break;
7794   case Instruction::ZExt:
7795   case Instruction::SExt:
7796     if (isa<ExtractElementInst>(I->getOperand(0)) ||
7797         isa<InsertElementInst>(I->getOperand(0)))
7798       return false;
7799     break;
7800 
7801   // We can demote certain binary operations if we can demote both of their
7802   // operands.
7803   case Instruction::Add:
7804   case Instruction::Sub:
7805   case Instruction::Mul:
7806   case Instruction::And:
7807   case Instruction::Or:
7808   case Instruction::Xor:
7809     if (!collectValuesToDemote(I->getOperand(0), Expr, ToDemote, Roots) ||
7810         !collectValuesToDemote(I->getOperand(1), Expr, ToDemote, Roots))
7811       return false;
7812     break;
7813 
7814   // We can demote selects if we can demote their true and false values.
7815   case Instruction::Select: {
7816     SelectInst *SI = cast<SelectInst>(I);
7817     if (!collectValuesToDemote(SI->getTrueValue(), Expr, ToDemote, Roots) ||
7818         !collectValuesToDemote(SI->getFalseValue(), Expr, ToDemote, Roots))
7819       return false;
7820     break;
7821   }
7822 
7823   // We can demote phis if we can demote all their incoming operands. Note that
7824   // we don't need to worry about cycles since we ensure single use above.
7825   case Instruction::PHI: {
7826     PHINode *PN = cast<PHINode>(I);
7827     for (Value *IncValue : PN->incoming_values())
7828       if (!collectValuesToDemote(IncValue, Expr, ToDemote, Roots))
7829         return false;
7830     break;
7831   }
7832 
7833   // Otherwise, conservatively give up.
7834   default:
7835     return false;
7836   }
7837 
7838   // Record the value that we can demote.
7839   ToDemote.push_back(V);
7840   return true;
7841 }
7842 
7843 void BoUpSLP::computeMinimumValueSizes() {
7844   // If there are no external uses, the expression tree must be rooted by a
7845   // store. We can't demote in-memory values, so there is nothing to do here.
7846   if (ExternalUses.empty())
7847     return;
7848 
7849   // We only attempt to truncate integer expressions.
7850   auto &TreeRoot = VectorizableTree[0]->Scalars;
7851   auto *TreeRootIT = dyn_cast<IntegerType>(TreeRoot[0]->getType());
7852   if (!TreeRootIT)
7853     return;
7854 
7855   // If the expression is not rooted by a store, these roots should have
7856   // external uses. We will rely on InstCombine to rewrite the expression in
7857   // the narrower type. However, InstCombine only rewrites single-use values.
7858   // This means that if a tree entry other than a root is used externally, it
7859   // must have multiple uses and InstCombine will not rewrite it. The code
7860   // below ensures that only the roots are used externally.
7861   SmallPtrSet<Value *, 32> Expr(TreeRoot.begin(), TreeRoot.end());
7862   for (auto &EU : ExternalUses)
7863     if (!Expr.erase(EU.Scalar))
7864       return;
7865   if (!Expr.empty())
7866     return;
7867 
7868   // Collect the scalar values of the vectorizable expression. We will use this
7869   // context to determine which values can be demoted. If we see a truncation,
7870   // we mark it as seeding another demotion.
7871   for (auto &EntryPtr : VectorizableTree)
7872     Expr.insert(EntryPtr->Scalars.begin(), EntryPtr->Scalars.end());
7873 
7874   // Ensure the roots of the vectorizable tree don't form a cycle. They must
7875   // have a single external user that is not in the vectorizable tree.
7876   for (auto *Root : TreeRoot)
7877     if (!Root->hasOneUse() || Expr.count(*Root->user_begin()))
7878       return;
7879 
7880   // Conservatively determine if we can actually truncate the roots of the
7881   // expression. Collect the values that can be demoted in ToDemote and
7882   // additional roots that require investigating in Roots.
7883   SmallVector<Value *, 32> ToDemote;
7884   SmallVector<Value *, 4> Roots;
7885   for (auto *Root : TreeRoot)
7886     if (!collectValuesToDemote(Root, Expr, ToDemote, Roots))
7887       return;
7888 
7889   // The maximum bit width required to represent all the values that can be
7890   // demoted without loss of precision. It would be safe to truncate the roots
7891   // of the expression to this width.
7892   auto MaxBitWidth = 8u;
7893 
7894   // We first check if all the bits of the roots are demanded. If they're not,
7895   // we can truncate the roots to this narrower type.
7896   for (auto *Root : TreeRoot) {
7897     auto Mask = DB->getDemandedBits(cast<Instruction>(Root));
7898     MaxBitWidth = std::max<unsigned>(
7899         Mask.getBitWidth() - Mask.countLeadingZeros(), MaxBitWidth);
7900   }
7901 
7902   // True if the roots can be zero-extended back to their original type, rather
7903   // than sign-extended. We know that if the leading bits are not demanded, we
7904   // can safely zero-extend. So we initialize IsKnownPositive to True.
7905   bool IsKnownPositive = true;
7906 
7907   // If all the bits of the roots are demanded, we can try a little harder to
7908   // compute a narrower type. This can happen, for example, if the roots are
7909   // getelementptr indices. InstCombine promotes these indices to the pointer
7910   // width. Thus, all their bits are technically demanded even though the
7911   // address computation might be vectorized in a smaller type.
7912   //
7913   // We start by looking at each entry that can be demoted. We compute the
7914   // maximum bit width required to store the scalar by using ValueTracking to
7915   // compute the number of high-order bits we can truncate.
7916   if (MaxBitWidth == DL->getTypeSizeInBits(TreeRoot[0]->getType()) &&
7917       llvm::all_of(TreeRoot, [](Value *R) {
7918         assert(R->hasOneUse() && "Root should have only one use!");
7919         return isa<GetElementPtrInst>(R->user_back());
7920       })) {
7921     MaxBitWidth = 8u;
7922 
7923     // Determine if the sign bit of all the roots is known to be zero. If not,
7924     // IsKnownPositive is set to False.
7925     IsKnownPositive = llvm::all_of(TreeRoot, [&](Value *R) {
7926       KnownBits Known = computeKnownBits(R, *DL);
7927       return Known.isNonNegative();
7928     });
7929 
7930     // Determine the maximum number of bits required to store the scalar
7931     // values.
7932     for (auto *Scalar : ToDemote) {
7933       auto NumSignBits = ComputeNumSignBits(Scalar, *DL, 0, AC, nullptr, DT);
7934       auto NumTypeBits = DL->getTypeSizeInBits(Scalar->getType());
7935       MaxBitWidth = std::max<unsigned>(NumTypeBits - NumSignBits, MaxBitWidth);
7936     }
7937 
7938     // If we can't prove that the sign bit is zero, we must add one to the
7939     // maximum bit width to account for the unknown sign bit. This preserves
7940     // the existing sign bit so we can safely sign-extend the root back to the
7941     // original type. Otherwise, if we know the sign bit is zero, we will
7942     // zero-extend the root instead.
7943     //
7944     // FIXME: This is somewhat suboptimal, as there will be cases where adding
7945     //        one to the maximum bit width will yield a larger-than-necessary
7946     //        type. In general, we need to add an extra bit only if we can't
7947     //        prove that the upper bit of the original type is equal to the
7948     //        upper bit of the proposed smaller type. If these two bits are the
7949     //        same (either zero or one) we know that sign-extending from the
7950     //        smaller type will result in the same value. Here, since we can't
7951     //        yet prove this, we are just making the proposed smaller type
7952     //        larger to ensure correctness.
7953     if (!IsKnownPositive)
7954       ++MaxBitWidth;
7955   }
7956 
7957   // Round MaxBitWidth up to the next power-of-two.
7958   if (!isPowerOf2_64(MaxBitWidth))
7959     MaxBitWidth = NextPowerOf2(MaxBitWidth);
7960 
7961   // If the maximum bit width we compute is less than the with of the roots'
7962   // type, we can proceed with the narrowing. Otherwise, do nothing.
7963   if (MaxBitWidth >= TreeRootIT->getBitWidth())
7964     return;
7965 
7966   // If we can truncate the root, we must collect additional values that might
7967   // be demoted as a result. That is, those seeded by truncations we will
7968   // modify.
7969   while (!Roots.empty())
7970     collectValuesToDemote(Roots.pop_back_val(), Expr, ToDemote, Roots);
7971 
7972   // Finally, map the values we can demote to the maximum bit with we computed.
7973   for (auto *Scalar : ToDemote)
7974     MinBWs[Scalar] = std::make_pair(MaxBitWidth, !IsKnownPositive);
7975 }
7976 
7977 namespace {
7978 
7979 /// The SLPVectorizer Pass.
7980 struct SLPVectorizer : public FunctionPass {
7981   SLPVectorizerPass Impl;
7982 
7983   /// Pass identification, replacement for typeid
7984   static char ID;
7985 
7986   explicit SLPVectorizer() : FunctionPass(ID) {
7987     initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
7988   }
7989 
7990   bool doInitialization(Module &M) override { return false; }
7991 
7992   bool runOnFunction(Function &F) override {
7993     if (skipFunction(F))
7994       return false;
7995 
7996     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
7997     auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
7998     auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
7999     auto *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
8000     auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
8001     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
8002     auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
8003     auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
8004     auto *DB = &getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
8005     auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
8006 
8007     return Impl.runImpl(F, SE, TTI, TLI, AA, LI, DT, AC, DB, ORE);
8008   }
8009 
8010   void getAnalysisUsage(AnalysisUsage &AU) const override {
8011     FunctionPass::getAnalysisUsage(AU);
8012     AU.addRequired<AssumptionCacheTracker>();
8013     AU.addRequired<ScalarEvolutionWrapperPass>();
8014     AU.addRequired<AAResultsWrapperPass>();
8015     AU.addRequired<TargetTransformInfoWrapperPass>();
8016     AU.addRequired<LoopInfoWrapperPass>();
8017     AU.addRequired<DominatorTreeWrapperPass>();
8018     AU.addRequired<DemandedBitsWrapperPass>();
8019     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
8020     AU.addRequired<InjectTLIMappingsLegacy>();
8021     AU.addPreserved<LoopInfoWrapperPass>();
8022     AU.addPreserved<DominatorTreeWrapperPass>();
8023     AU.addPreserved<AAResultsWrapperPass>();
8024     AU.addPreserved<GlobalsAAWrapperPass>();
8025     AU.setPreservesCFG();
8026   }
8027 };
8028 
8029 } // end anonymous namespace
8030 
8031 PreservedAnalyses SLPVectorizerPass::run(Function &F, FunctionAnalysisManager &AM) {
8032   auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
8033   auto *TTI = &AM.getResult<TargetIRAnalysis>(F);
8034   auto *TLI = AM.getCachedResult<TargetLibraryAnalysis>(F);
8035   auto *AA = &AM.getResult<AAManager>(F);
8036   auto *LI = &AM.getResult<LoopAnalysis>(F);
8037   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
8038   auto *AC = &AM.getResult<AssumptionAnalysis>(F);
8039   auto *DB = &AM.getResult<DemandedBitsAnalysis>(F);
8040   auto *ORE = &AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
8041 
8042   bool Changed = runImpl(F, SE, TTI, TLI, AA, LI, DT, AC, DB, ORE);
8043   if (!Changed)
8044     return PreservedAnalyses::all();
8045 
8046   PreservedAnalyses PA;
8047   PA.preserveSet<CFGAnalyses>();
8048   return PA;
8049 }
8050 
8051 bool SLPVectorizerPass::runImpl(Function &F, ScalarEvolution *SE_,
8052                                 TargetTransformInfo *TTI_,
8053                                 TargetLibraryInfo *TLI_, AAResults *AA_,
8054                                 LoopInfo *LI_, DominatorTree *DT_,
8055                                 AssumptionCache *AC_, DemandedBits *DB_,
8056                                 OptimizationRemarkEmitter *ORE_) {
8057   if (!RunSLPVectorization)
8058     return false;
8059   SE = SE_;
8060   TTI = TTI_;
8061   TLI = TLI_;
8062   AA = AA_;
8063   LI = LI_;
8064   DT = DT_;
8065   AC = AC_;
8066   DB = DB_;
8067   DL = &F.getParent()->getDataLayout();
8068 
8069   Stores.clear();
8070   GEPs.clear();
8071   bool Changed = false;
8072 
8073   // If the target claims to have no vector registers don't attempt
8074   // vectorization.
8075   if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true)))
8076     return false;
8077 
8078   // Don't vectorize when the attribute NoImplicitFloat is used.
8079   if (F.hasFnAttribute(Attribute::NoImplicitFloat))
8080     return false;
8081 
8082   LLVM_DEBUG(dbgs() << "SLP: Analyzing blocks in " << F.getName() << ".\n");
8083 
8084   // Use the bottom up slp vectorizer to construct chains that start with
8085   // store instructions.
8086   BoUpSLP R(&F, SE, TTI, TLI, AA, LI, DT, AC, DB, DL, ORE_);
8087 
8088   // A general note: the vectorizer must use BoUpSLP::eraseInstruction() to
8089   // delete instructions.
8090 
8091   // Update DFS numbers now so that we can use them for ordering.
8092   DT->updateDFSNumbers();
8093 
8094   // Scan the blocks in the function in post order.
8095   for (auto BB : post_order(&F.getEntryBlock())) {
8096     collectSeedInstructions(BB);
8097 
8098     // Vectorize trees that end at stores.
8099     if (!Stores.empty()) {
8100       LLVM_DEBUG(dbgs() << "SLP: Found stores for " << Stores.size()
8101                         << " underlying objects.\n");
8102       Changed |= vectorizeStoreChains(R);
8103     }
8104 
8105     // Vectorize trees that end at reductions.
8106     Changed |= vectorizeChainsInBlock(BB, R);
8107 
8108     // Vectorize the index computations of getelementptr instructions. This
8109     // is primarily intended to catch gather-like idioms ending at
8110     // non-consecutive loads.
8111     if (!GEPs.empty()) {
8112       LLVM_DEBUG(dbgs() << "SLP: Found GEPs for " << GEPs.size()
8113                         << " underlying objects.\n");
8114       Changed |= vectorizeGEPIndices(BB, R);
8115     }
8116   }
8117 
8118   if (Changed) {
8119     R.optimizeGatherSequence();
8120     LLVM_DEBUG(dbgs() << "SLP: vectorized \"" << F.getName() << "\"\n");
8121   }
8122   return Changed;
8123 }
8124 
8125 bool SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
8126                                             unsigned Idx) {
8127   LLVM_DEBUG(dbgs() << "SLP: Analyzing a store chain of length " << Chain.size()
8128                     << "\n");
8129   const unsigned Sz = R.getVectorElementSize(Chain[0]);
8130   const unsigned MinVF = R.getMinVecRegSize() / Sz;
8131   unsigned VF = Chain.size();
8132 
8133   if (!isPowerOf2_32(Sz) || !isPowerOf2_32(VF) || VF < 2 || VF < MinVF)
8134     return false;
8135 
8136   LLVM_DEBUG(dbgs() << "SLP: Analyzing " << VF << " stores at offset " << Idx
8137                     << "\n");
8138 
8139   R.buildTree(Chain);
8140   if (R.isTreeTinyAndNotFullyVectorizable())
8141     return false;
8142   if (R.isLoadCombineCandidate())
8143     return false;
8144   R.reorderTopToBottom();
8145   R.reorderBottomToTop();
8146   R.buildExternalUses();
8147 
8148   R.computeMinimumValueSizes();
8149 
8150   InstructionCost Cost = R.getTreeCost();
8151 
8152   LLVM_DEBUG(dbgs() << "SLP: Found cost = " << Cost << " for VF =" << VF << "\n");
8153   if (Cost < -SLPCostThreshold) {
8154     LLVM_DEBUG(dbgs() << "SLP: Decided to vectorize cost = " << Cost << "\n");
8155 
8156     using namespace ore;
8157 
8158     R.getORE()->emit(OptimizationRemark(SV_NAME, "StoresVectorized",
8159                                         cast<StoreInst>(Chain[0]))
8160                      << "Stores SLP vectorized with cost " << NV("Cost", Cost)
8161                      << " and with tree size "
8162                      << NV("TreeSize", R.getTreeSize()));
8163 
8164     R.vectorizeTree();
8165     return true;
8166   }
8167 
8168   return false;
8169 }
8170 
8171 bool SLPVectorizerPass::vectorizeStores(ArrayRef<StoreInst *> Stores,
8172                                         BoUpSLP &R) {
8173   // We may run into multiple chains that merge into a single chain. We mark the
8174   // stores that we vectorized so that we don't visit the same store twice.
8175   BoUpSLP::ValueSet VectorizedStores;
8176   bool Changed = false;
8177 
8178   int E = Stores.size();
8179   SmallBitVector Tails(E, false);
8180   int MaxIter = MaxStoreLookup.getValue();
8181   SmallVector<std::pair<int, int>, 16> ConsecutiveChain(
8182       E, std::make_pair(E, INT_MAX));
8183   SmallVector<SmallBitVector, 4> CheckedPairs(E, SmallBitVector(E, false));
8184   int IterCnt;
8185   auto &&FindConsecutiveAccess = [this, &Stores, &Tails, &IterCnt, MaxIter,
8186                                   &CheckedPairs,
8187                                   &ConsecutiveChain](int K, int Idx) {
8188     if (IterCnt >= MaxIter)
8189       return true;
8190     if (CheckedPairs[Idx].test(K))
8191       return ConsecutiveChain[K].second == 1 &&
8192              ConsecutiveChain[K].first == Idx;
8193     ++IterCnt;
8194     CheckedPairs[Idx].set(K);
8195     CheckedPairs[K].set(Idx);
8196     Optional<int> Diff = getPointersDiff(
8197         Stores[K]->getValueOperand()->getType(), Stores[K]->getPointerOperand(),
8198         Stores[Idx]->getValueOperand()->getType(),
8199         Stores[Idx]->getPointerOperand(), *DL, *SE, /*StrictCheck=*/true);
8200     if (!Diff || *Diff == 0)
8201       return false;
8202     int Val = *Diff;
8203     if (Val < 0) {
8204       if (ConsecutiveChain[Idx].second > -Val) {
8205         Tails.set(K);
8206         ConsecutiveChain[Idx] = std::make_pair(K, -Val);
8207       }
8208       return false;
8209     }
8210     if (ConsecutiveChain[K].second <= Val)
8211       return false;
8212 
8213     Tails.set(Idx);
8214     ConsecutiveChain[K] = std::make_pair(Idx, Val);
8215     return Val == 1;
8216   };
8217   // Do a quadratic search on all of the given stores in reverse order and find
8218   // all of the pairs of stores that follow each other.
8219   for (int Idx = E - 1; Idx >= 0; --Idx) {
8220     // If a store has multiple consecutive store candidates, search according
8221     // to the sequence: Idx-1, Idx+1, Idx-2, Idx+2, ...
8222     // This is because usually pairing with immediate succeeding or preceding
8223     // candidate create the best chance to find slp vectorization opportunity.
8224     const int MaxLookDepth = std::max(E - Idx, Idx + 1);
8225     IterCnt = 0;
8226     for (int Offset = 1, F = MaxLookDepth; Offset < F; ++Offset)
8227       if ((Idx >= Offset && FindConsecutiveAccess(Idx - Offset, Idx)) ||
8228           (Idx + Offset < E && FindConsecutiveAccess(Idx + Offset, Idx)))
8229         break;
8230   }
8231 
8232   // Tracks if we tried to vectorize stores starting from the given tail
8233   // already.
8234   SmallBitVector TriedTails(E, false);
8235   // For stores that start but don't end a link in the chain:
8236   for (int Cnt = E; Cnt > 0; --Cnt) {
8237     int I = Cnt - 1;
8238     if (ConsecutiveChain[I].first == E || Tails.test(I))
8239       continue;
8240     // We found a store instr that starts a chain. Now follow the chain and try
8241     // to vectorize it.
8242     BoUpSLP::ValueList Operands;
8243     // Collect the chain into a list.
8244     while (I != E && !VectorizedStores.count(Stores[I])) {
8245       Operands.push_back(Stores[I]);
8246       Tails.set(I);
8247       if (ConsecutiveChain[I].second != 1) {
8248         // Mark the new end in the chain and go back, if required. It might be
8249         // required if the original stores come in reversed order, for example.
8250         if (ConsecutiveChain[I].first != E &&
8251             Tails.test(ConsecutiveChain[I].first) && !TriedTails.test(I) &&
8252             !VectorizedStores.count(Stores[ConsecutiveChain[I].first])) {
8253           TriedTails.set(I);
8254           Tails.reset(ConsecutiveChain[I].first);
8255           if (Cnt < ConsecutiveChain[I].first + 2)
8256             Cnt = ConsecutiveChain[I].first + 2;
8257         }
8258         break;
8259       }
8260       // Move to the next value in the chain.
8261       I = ConsecutiveChain[I].first;
8262     }
8263     assert(!Operands.empty() && "Expected non-empty list of stores.");
8264 
8265     unsigned MaxVecRegSize = R.getMaxVecRegSize();
8266     unsigned EltSize = R.getVectorElementSize(Operands[0]);
8267     unsigned MaxElts = llvm::PowerOf2Floor(MaxVecRegSize / EltSize);
8268 
8269     unsigned MinVF = R.getMinVF(EltSize);
8270     unsigned MaxVF = std::min(R.getMaximumVF(EltSize, Instruction::Store),
8271                               MaxElts);
8272 
8273     // FIXME: Is division-by-2 the correct step? Should we assert that the
8274     // register size is a power-of-2?
8275     unsigned StartIdx = 0;
8276     for (unsigned Size = MaxVF; Size >= MinVF; Size /= 2) {
8277       for (unsigned Cnt = StartIdx, E = Operands.size(); Cnt + Size <= E;) {
8278         ArrayRef<Value *> Slice = makeArrayRef(Operands).slice(Cnt, Size);
8279         if (!VectorizedStores.count(Slice.front()) &&
8280             !VectorizedStores.count(Slice.back()) &&
8281             vectorizeStoreChain(Slice, R, Cnt)) {
8282           // Mark the vectorized stores so that we don't vectorize them again.
8283           VectorizedStores.insert(Slice.begin(), Slice.end());
8284           Changed = true;
8285           // If we vectorized initial block, no need to try to vectorize it
8286           // again.
8287           if (Cnt == StartIdx)
8288             StartIdx += Size;
8289           Cnt += Size;
8290           continue;
8291         }
8292         ++Cnt;
8293       }
8294       // Check if the whole array was vectorized already - exit.
8295       if (StartIdx >= Operands.size())
8296         break;
8297     }
8298   }
8299 
8300   return Changed;
8301 }
8302 
8303 void SLPVectorizerPass::collectSeedInstructions(BasicBlock *BB) {
8304   // Initialize the collections. We will make a single pass over the block.
8305   Stores.clear();
8306   GEPs.clear();
8307 
8308   // Visit the store and getelementptr instructions in BB and organize them in
8309   // Stores and GEPs according to the underlying objects of their pointer
8310   // operands.
8311   for (Instruction &I : *BB) {
8312     // Ignore store instructions that are volatile or have a pointer operand
8313     // that doesn't point to a scalar type.
8314     if (auto *SI = dyn_cast<StoreInst>(&I)) {
8315       if (!SI->isSimple())
8316         continue;
8317       if (!isValidElementType(SI->getValueOperand()->getType()))
8318         continue;
8319       Stores[getUnderlyingObject(SI->getPointerOperand())].push_back(SI);
8320     }
8321 
8322     // Ignore getelementptr instructions that have more than one index, a
8323     // constant index, or a pointer operand that doesn't point to a scalar
8324     // type.
8325     else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
8326       auto Idx = GEP->idx_begin()->get();
8327       if (GEP->getNumIndices() > 1 || isa<Constant>(Idx))
8328         continue;
8329       if (!isValidElementType(Idx->getType()))
8330         continue;
8331       if (GEP->getType()->isVectorTy())
8332         continue;
8333       GEPs[GEP->getPointerOperand()].push_back(GEP);
8334     }
8335   }
8336 }
8337 
8338 bool SLPVectorizerPass::tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) {
8339   if (!A || !B)
8340     return false;
8341   Value *VL[] = {A, B};
8342   return tryToVectorizeList(VL, R);
8343 }
8344 
8345 bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
8346                                            bool LimitForRegisterSize) {
8347   if (VL.size() < 2)
8348     return false;
8349 
8350   LLVM_DEBUG(dbgs() << "SLP: Trying to vectorize a list of length = "
8351                     << VL.size() << ".\n");
8352 
8353   // Check that all of the parts are instructions of the same type,
8354   // we permit an alternate opcode via InstructionsState.
8355   InstructionsState S = getSameOpcode(VL);
8356   if (!S.getOpcode())
8357     return false;
8358 
8359   Instruction *I0 = cast<Instruction>(S.OpValue);
8360   // Make sure invalid types (including vector type) are rejected before
8361   // determining vectorization factor for scalar instructions.
8362   for (Value *V : VL) {
8363     Type *Ty = V->getType();
8364     if (!isa<InsertElementInst>(V) && !isValidElementType(Ty)) {
8365       // NOTE: the following will give user internal llvm type name, which may
8366       // not be useful.
8367       R.getORE()->emit([&]() {
8368         std::string type_str;
8369         llvm::raw_string_ostream rso(type_str);
8370         Ty->print(rso);
8371         return OptimizationRemarkMissed(SV_NAME, "UnsupportedType", I0)
8372                << "Cannot SLP vectorize list: type "
8373                << rso.str() + " is unsupported by vectorizer";
8374       });
8375       return false;
8376     }
8377   }
8378 
8379   unsigned Sz = R.getVectorElementSize(I0);
8380   unsigned MinVF = R.getMinVF(Sz);
8381   unsigned MaxVF = std::max<unsigned>(PowerOf2Floor(VL.size()), MinVF);
8382   MaxVF = std::min(R.getMaximumVF(Sz, S.getOpcode()), MaxVF);
8383   if (MaxVF < 2) {
8384     R.getORE()->emit([&]() {
8385       return OptimizationRemarkMissed(SV_NAME, "SmallVF", I0)
8386              << "Cannot SLP vectorize list: vectorization factor "
8387              << "less than 2 is not supported";
8388     });
8389     return false;
8390   }
8391 
8392   bool Changed = false;
8393   bool CandidateFound = false;
8394   InstructionCost MinCost = SLPCostThreshold.getValue();
8395   Type *ScalarTy = VL[0]->getType();
8396   if (auto *IE = dyn_cast<InsertElementInst>(VL[0]))
8397     ScalarTy = IE->getOperand(1)->getType();
8398 
8399   unsigned NextInst = 0, MaxInst = VL.size();
8400   for (unsigned VF = MaxVF; NextInst + 1 < MaxInst && VF >= MinVF; VF /= 2) {
8401     // No actual vectorization should happen, if number of parts is the same as
8402     // provided vectorization factor (i.e. the scalar type is used for vector
8403     // code during codegen).
8404     auto *VecTy = FixedVectorType::get(ScalarTy, VF);
8405     if (TTI->getNumberOfParts(VecTy) == VF)
8406       continue;
8407     for (unsigned I = NextInst; I < MaxInst; ++I) {
8408       unsigned OpsWidth = 0;
8409 
8410       if (I + VF > MaxInst)
8411         OpsWidth = MaxInst - I;
8412       else
8413         OpsWidth = VF;
8414 
8415       if (!isPowerOf2_32(OpsWidth))
8416         continue;
8417 
8418       if ((LimitForRegisterSize && OpsWidth < MaxVF) ||
8419           (VF > MinVF && OpsWidth <= VF / 2) || (VF == MinVF && OpsWidth < 2))
8420         break;
8421 
8422       ArrayRef<Value *> Ops = VL.slice(I, OpsWidth);
8423       // Check that a previous iteration of this loop did not delete the Value.
8424       if (llvm::any_of(Ops, [&R](Value *V) {
8425             auto *I = dyn_cast<Instruction>(V);
8426             return I && R.isDeleted(I);
8427           }))
8428         continue;
8429 
8430       LLVM_DEBUG(dbgs() << "SLP: Analyzing " << OpsWidth << " operations "
8431                         << "\n");
8432 
8433       R.buildTree(Ops);
8434       if (R.isTreeTinyAndNotFullyVectorizable())
8435         continue;
8436       R.reorderTopToBottom();
8437       R.reorderBottomToTop();
8438       R.buildExternalUses();
8439 
8440       R.computeMinimumValueSizes();
8441       InstructionCost Cost = R.getTreeCost();
8442       CandidateFound = true;
8443       MinCost = std::min(MinCost, Cost);
8444 
8445       if (Cost < -SLPCostThreshold) {
8446         LLVM_DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
8447         R.getORE()->emit(OptimizationRemark(SV_NAME, "VectorizedList",
8448                                                     cast<Instruction>(Ops[0]))
8449                                  << "SLP vectorized with cost " << ore::NV("Cost", Cost)
8450                                  << " and with tree size "
8451                                  << ore::NV("TreeSize", R.getTreeSize()));
8452 
8453         R.vectorizeTree();
8454         // Move to the next bundle.
8455         I += VF - 1;
8456         NextInst = I + 1;
8457         Changed = true;
8458       }
8459     }
8460   }
8461 
8462   if (!Changed && CandidateFound) {
8463     R.getORE()->emit([&]() {
8464       return OptimizationRemarkMissed(SV_NAME, "NotBeneficial", I0)
8465              << "List vectorization was possible but not beneficial with cost "
8466              << ore::NV("Cost", MinCost) << " >= "
8467              << ore::NV("Treshold", -SLPCostThreshold);
8468     });
8469   } else if (!Changed) {
8470     R.getORE()->emit([&]() {
8471       return OptimizationRemarkMissed(SV_NAME, "NotPossible", I0)
8472              << "Cannot SLP vectorize list: vectorization was impossible"
8473              << " with available vectorization factors";
8474     });
8475   }
8476   return Changed;
8477 }
8478 
8479 bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) {
8480   if (!I)
8481     return false;
8482 
8483   if (!isa<BinaryOperator>(I) && !isa<CmpInst>(I))
8484     return false;
8485 
8486   Value *P = I->getParent();
8487 
8488   // Vectorize in current basic block only.
8489   auto *Op0 = dyn_cast<Instruction>(I->getOperand(0));
8490   auto *Op1 = dyn_cast<Instruction>(I->getOperand(1));
8491   if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P)
8492     return false;
8493 
8494   // Try to vectorize V.
8495   if (tryToVectorizePair(Op0, Op1, R))
8496     return true;
8497 
8498   auto *A = dyn_cast<BinaryOperator>(Op0);
8499   auto *B = dyn_cast<BinaryOperator>(Op1);
8500   // Try to skip B.
8501   if (B && B->hasOneUse()) {
8502     auto *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
8503     auto *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
8504     if (B0 && B0->getParent() == P && tryToVectorizePair(A, B0, R))
8505       return true;
8506     if (B1 && B1->getParent() == P && tryToVectorizePair(A, B1, R))
8507       return true;
8508   }
8509 
8510   // Try to skip A.
8511   if (A && A->hasOneUse()) {
8512     auto *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
8513     auto *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
8514     if (A0 && A0->getParent() == P && tryToVectorizePair(A0, B, R))
8515       return true;
8516     if (A1 && A1->getParent() == P && tryToVectorizePair(A1, B, R))
8517       return true;
8518   }
8519   return false;
8520 }
8521 
8522 namespace {
8523 
8524 /// Model horizontal reductions.
8525 ///
8526 /// A horizontal reduction is a tree of reduction instructions that has values
8527 /// that can be put into a vector as its leaves. For example:
8528 ///
8529 /// mul mul mul mul
8530 ///  \  /    \  /
8531 ///   +       +
8532 ///    \     /
8533 ///       +
8534 /// This tree has "mul" as its leaf values and "+" as its reduction
8535 /// instructions. A reduction can feed into a store or a binary operation
8536 /// feeding a phi.
8537 ///    ...
8538 ///    \  /
8539 ///     +
8540 ///     |
8541 ///  phi +=
8542 ///
8543 ///  Or:
8544 ///    ...
8545 ///    \  /
8546 ///     +
8547 ///     |
8548 ///   *p =
8549 ///
8550 class HorizontalReduction {
8551   using ReductionOpsType = SmallVector<Value *, 16>;
8552   using ReductionOpsListType = SmallVector<ReductionOpsType, 2>;
8553   ReductionOpsListType ReductionOps;
8554   SmallVector<Value *, 32> ReducedVals;
8555   // Use map vector to make stable output.
8556   MapVector<Instruction *, Value *> ExtraArgs;
8557   WeakTrackingVH ReductionRoot;
8558   /// The type of reduction operation.
8559   RecurKind RdxKind;
8560 
8561   const unsigned INVALID_OPERAND_INDEX = std::numeric_limits<unsigned>::max();
8562 
8563   static bool isCmpSelMinMax(Instruction *I) {
8564     return match(I, m_Select(m_Cmp(), m_Value(), m_Value())) &&
8565            RecurrenceDescriptor::isMinMaxRecurrenceKind(getRdxKind(I));
8566   }
8567 
8568   // And/or are potentially poison-safe logical patterns like:
8569   // select x, y, false
8570   // select x, true, y
8571   static bool isBoolLogicOp(Instruction *I) {
8572     return match(I, m_LogicalAnd(m_Value(), m_Value())) ||
8573            match(I, m_LogicalOr(m_Value(), m_Value()));
8574   }
8575 
8576   /// Checks if instruction is associative and can be vectorized.
8577   static bool isVectorizable(RecurKind Kind, Instruction *I) {
8578     if (Kind == RecurKind::None)
8579       return false;
8580 
8581     // Integer ops that map to select instructions or intrinsics are fine.
8582     if (RecurrenceDescriptor::isIntMinMaxRecurrenceKind(Kind) ||
8583         isBoolLogicOp(I))
8584       return true;
8585 
8586     if (Kind == RecurKind::FMax || Kind == RecurKind::FMin) {
8587       // FP min/max are associative except for NaN and -0.0. We do not
8588       // have to rule out -0.0 here because the intrinsic semantics do not
8589       // specify a fixed result for it.
8590       return I->getFastMathFlags().noNaNs();
8591     }
8592 
8593     return I->isAssociative();
8594   }
8595 
8596   static Value *getRdxOperand(Instruction *I, unsigned Index) {
8597     // Poison-safe 'or' takes the form: select X, true, Y
8598     // To make that work with the normal operand processing, we skip the
8599     // true value operand.
8600     // TODO: Change the code and data structures to handle this without a hack.
8601     if (getRdxKind(I) == RecurKind::Or && isa<SelectInst>(I) && Index == 1)
8602       return I->getOperand(2);
8603     return I->getOperand(Index);
8604   }
8605 
8606   /// Checks if the ParentStackElem.first should be marked as a reduction
8607   /// operation with an extra argument or as extra argument itself.
8608   void markExtraArg(std::pair<Instruction *, unsigned> &ParentStackElem,
8609                     Value *ExtraArg) {
8610     if (ExtraArgs.count(ParentStackElem.first)) {
8611       ExtraArgs[ParentStackElem.first] = nullptr;
8612       // We ran into something like:
8613       // ParentStackElem.first = ExtraArgs[ParentStackElem.first] + ExtraArg.
8614       // The whole ParentStackElem.first should be considered as an extra value
8615       // in this case.
8616       // Do not perform analysis of remaining operands of ParentStackElem.first
8617       // instruction, this whole instruction is an extra argument.
8618       ParentStackElem.second = INVALID_OPERAND_INDEX;
8619     } else {
8620       // We ran into something like:
8621       // ParentStackElem.first += ... + ExtraArg + ...
8622       ExtraArgs[ParentStackElem.first] = ExtraArg;
8623     }
8624   }
8625 
8626   /// Creates reduction operation with the current opcode.
8627   static Value *createOp(IRBuilder<> &Builder, RecurKind Kind, Value *LHS,
8628                          Value *RHS, const Twine &Name, bool UseSelect) {
8629     unsigned RdxOpcode = RecurrenceDescriptor::getOpcode(Kind);
8630     switch (Kind) {
8631     case RecurKind::Or:
8632       if (UseSelect &&
8633           LHS->getType() == CmpInst::makeCmpResultType(LHS->getType()))
8634         return Builder.CreateSelect(LHS, Builder.getTrue(), RHS, Name);
8635       return Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, LHS, RHS,
8636                                  Name);
8637     case RecurKind::And:
8638       if (UseSelect &&
8639           LHS->getType() == CmpInst::makeCmpResultType(LHS->getType()))
8640         return Builder.CreateSelect(LHS, RHS, Builder.getFalse(), Name);
8641       return Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, LHS, RHS,
8642                                  Name);
8643     case RecurKind::Add:
8644     case RecurKind::Mul:
8645     case RecurKind::Xor:
8646     case RecurKind::FAdd:
8647     case RecurKind::FMul:
8648       return Builder.CreateBinOp((Instruction::BinaryOps)RdxOpcode, LHS, RHS,
8649                                  Name);
8650     case RecurKind::FMax:
8651       return Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS);
8652     case RecurKind::FMin:
8653       return Builder.CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS);
8654     case RecurKind::SMax:
8655       if (UseSelect) {
8656         Value *Cmp = Builder.CreateICmpSGT(LHS, RHS, Name);
8657         return Builder.CreateSelect(Cmp, LHS, RHS, Name);
8658       }
8659       return Builder.CreateBinaryIntrinsic(Intrinsic::smax, LHS, RHS);
8660     case RecurKind::SMin:
8661       if (UseSelect) {
8662         Value *Cmp = Builder.CreateICmpSLT(LHS, RHS, Name);
8663         return Builder.CreateSelect(Cmp, LHS, RHS, Name);
8664       }
8665       return Builder.CreateBinaryIntrinsic(Intrinsic::smin, LHS, RHS);
8666     case RecurKind::UMax:
8667       if (UseSelect) {
8668         Value *Cmp = Builder.CreateICmpUGT(LHS, RHS, Name);
8669         return Builder.CreateSelect(Cmp, LHS, RHS, Name);
8670       }
8671       return Builder.CreateBinaryIntrinsic(Intrinsic::umax, LHS, RHS);
8672     case RecurKind::UMin:
8673       if (UseSelect) {
8674         Value *Cmp = Builder.CreateICmpULT(LHS, RHS, Name);
8675         return Builder.CreateSelect(Cmp, LHS, RHS, Name);
8676       }
8677       return Builder.CreateBinaryIntrinsic(Intrinsic::umin, LHS, RHS);
8678     default:
8679       llvm_unreachable("Unknown reduction operation.");
8680     }
8681   }
8682 
8683   /// Creates reduction operation with the current opcode with the IR flags
8684   /// from \p ReductionOps.
8685   static Value *createOp(IRBuilder<> &Builder, RecurKind RdxKind, Value *LHS,
8686                          Value *RHS, const Twine &Name,
8687                          const ReductionOpsListType &ReductionOps) {
8688     bool UseSelect = ReductionOps.size() == 2 ||
8689                      // Logical or/and.
8690                      (ReductionOps.size() == 1 &&
8691                       isa<SelectInst>(ReductionOps.front().front()));
8692     assert((!UseSelect || ReductionOps.size() != 2 ||
8693             isa<SelectInst>(ReductionOps[1][0])) &&
8694            "Expected cmp + select pairs for reduction");
8695     Value *Op = createOp(Builder, RdxKind, LHS, RHS, Name, UseSelect);
8696     if (RecurrenceDescriptor::isIntMinMaxRecurrenceKind(RdxKind)) {
8697       if (auto *Sel = dyn_cast<SelectInst>(Op)) {
8698         propagateIRFlags(Sel->getCondition(), ReductionOps[0]);
8699         propagateIRFlags(Op, ReductionOps[1]);
8700         return Op;
8701       }
8702     }
8703     propagateIRFlags(Op, ReductionOps[0]);
8704     return Op;
8705   }
8706 
8707   /// Creates reduction operation with the current opcode with the IR flags
8708   /// from \p I.
8709   static Value *createOp(IRBuilder<> &Builder, RecurKind RdxKind, Value *LHS,
8710                          Value *RHS, const Twine &Name, Instruction *I) {
8711     auto *SelI = dyn_cast<SelectInst>(I);
8712     Value *Op = createOp(Builder, RdxKind, LHS, RHS, Name, SelI != nullptr);
8713     if (SelI && RecurrenceDescriptor::isIntMinMaxRecurrenceKind(RdxKind)) {
8714       if (auto *Sel = dyn_cast<SelectInst>(Op))
8715         propagateIRFlags(Sel->getCondition(), SelI->getCondition());
8716     }
8717     propagateIRFlags(Op, I);
8718     return Op;
8719   }
8720 
8721   static RecurKind getRdxKind(Instruction *I) {
8722     assert(I && "Expected instruction for reduction matching");
8723     TargetTransformInfo::ReductionFlags RdxFlags;
8724     if (match(I, m_Add(m_Value(), m_Value())))
8725       return RecurKind::Add;
8726     if (match(I, m_Mul(m_Value(), m_Value())))
8727       return RecurKind::Mul;
8728     if (match(I, m_And(m_Value(), m_Value())) ||
8729         match(I, m_LogicalAnd(m_Value(), m_Value())))
8730       return RecurKind::And;
8731     if (match(I, m_Or(m_Value(), m_Value())) ||
8732         match(I, m_LogicalOr(m_Value(), m_Value())))
8733       return RecurKind::Or;
8734     if (match(I, m_Xor(m_Value(), m_Value())))
8735       return RecurKind::Xor;
8736     if (match(I, m_FAdd(m_Value(), m_Value())))
8737       return RecurKind::FAdd;
8738     if (match(I, m_FMul(m_Value(), m_Value())))
8739       return RecurKind::FMul;
8740 
8741     if (match(I, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_Value())))
8742       return RecurKind::FMax;
8743     if (match(I, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_Value())))
8744       return RecurKind::FMin;
8745 
8746     // This matches either cmp+select or intrinsics. SLP is expected to handle
8747     // either form.
8748     // TODO: If we are canonicalizing to intrinsics, we can remove several
8749     //       special-case paths that deal with selects.
8750     if (match(I, m_SMax(m_Value(), m_Value())))
8751       return RecurKind::SMax;
8752     if (match(I, m_SMin(m_Value(), m_Value())))
8753       return RecurKind::SMin;
8754     if (match(I, m_UMax(m_Value(), m_Value())))
8755       return RecurKind::UMax;
8756     if (match(I, m_UMin(m_Value(), m_Value())))
8757       return RecurKind::UMin;
8758 
8759     if (auto *Select = dyn_cast<SelectInst>(I)) {
8760       // Try harder: look for min/max pattern based on instructions producing
8761       // same values such as: select ((cmp Inst1, Inst2), Inst1, Inst2).
8762       // During the intermediate stages of SLP, it's very common to have
8763       // pattern like this (since optimizeGatherSequence is run only once
8764       // at the end):
8765       // %1 = extractelement <2 x i32> %a, i32 0
8766       // %2 = extractelement <2 x i32> %a, i32 1
8767       // %cond = icmp sgt i32 %1, %2
8768       // %3 = extractelement <2 x i32> %a, i32 0
8769       // %4 = extractelement <2 x i32> %a, i32 1
8770       // %select = select i1 %cond, i32 %3, i32 %4
8771       CmpInst::Predicate Pred;
8772       Instruction *L1;
8773       Instruction *L2;
8774 
8775       Value *LHS = Select->getTrueValue();
8776       Value *RHS = Select->getFalseValue();
8777       Value *Cond = Select->getCondition();
8778 
8779       // TODO: Support inverse predicates.
8780       if (match(Cond, m_Cmp(Pred, m_Specific(LHS), m_Instruction(L2)))) {
8781         if (!isa<ExtractElementInst>(RHS) ||
8782             !L2->isIdenticalTo(cast<Instruction>(RHS)))
8783           return RecurKind::None;
8784       } else if (match(Cond, m_Cmp(Pred, m_Instruction(L1), m_Specific(RHS)))) {
8785         if (!isa<ExtractElementInst>(LHS) ||
8786             !L1->isIdenticalTo(cast<Instruction>(LHS)))
8787           return RecurKind::None;
8788       } else {
8789         if (!isa<ExtractElementInst>(LHS) || !isa<ExtractElementInst>(RHS))
8790           return RecurKind::None;
8791         if (!match(Cond, m_Cmp(Pred, m_Instruction(L1), m_Instruction(L2))) ||
8792             !L1->isIdenticalTo(cast<Instruction>(LHS)) ||
8793             !L2->isIdenticalTo(cast<Instruction>(RHS)))
8794           return RecurKind::None;
8795       }
8796 
8797       TargetTransformInfo::ReductionFlags RdxFlags;
8798       switch (Pred) {
8799       default:
8800         return RecurKind::None;
8801       case CmpInst::ICMP_SGT:
8802       case CmpInst::ICMP_SGE:
8803         return RecurKind::SMax;
8804       case CmpInst::ICMP_SLT:
8805       case CmpInst::ICMP_SLE:
8806         return RecurKind::SMin;
8807       case CmpInst::ICMP_UGT:
8808       case CmpInst::ICMP_UGE:
8809         return RecurKind::UMax;
8810       case CmpInst::ICMP_ULT:
8811       case CmpInst::ICMP_ULE:
8812         return RecurKind::UMin;
8813       }
8814     }
8815     return RecurKind::None;
8816   }
8817 
8818   /// Get the index of the first operand.
8819   static unsigned getFirstOperandIndex(Instruction *I) {
8820     return isCmpSelMinMax(I) ? 1 : 0;
8821   }
8822 
8823   /// Total number of operands in the reduction operation.
8824   static unsigned getNumberOfOperands(Instruction *I) {
8825     return isCmpSelMinMax(I) ? 3 : 2;
8826   }
8827 
8828   /// Checks if the instruction is in basic block \p BB.
8829   /// For a cmp+sel min/max reduction check that both ops are in \p BB.
8830   static bool hasSameParent(Instruction *I, BasicBlock *BB) {
8831     if (isCmpSelMinMax(I) || (isBoolLogicOp(I) && isa<SelectInst>(I))) {
8832       auto *Sel = cast<SelectInst>(I);
8833       auto *Cmp = dyn_cast<Instruction>(Sel->getCondition());
8834       return Sel->getParent() == BB && Cmp && Cmp->getParent() == BB;
8835     }
8836     return I->getParent() == BB;
8837   }
8838 
8839   /// Expected number of uses for reduction operations/reduced values.
8840   static bool hasRequiredNumberOfUses(bool IsCmpSelMinMax, Instruction *I) {
8841     if (IsCmpSelMinMax) {
8842       // SelectInst must be used twice while the condition op must have single
8843       // use only.
8844       if (auto *Sel = dyn_cast<SelectInst>(I))
8845         return Sel->hasNUses(2) && Sel->getCondition()->hasOneUse();
8846       return I->hasNUses(2);
8847     }
8848 
8849     // Arithmetic reduction operation must be used once only.
8850     return I->hasOneUse();
8851   }
8852 
8853   /// Initializes the list of reduction operations.
8854   void initReductionOps(Instruction *I) {
8855     if (isCmpSelMinMax(I))
8856       ReductionOps.assign(2, ReductionOpsType());
8857     else
8858       ReductionOps.assign(1, ReductionOpsType());
8859   }
8860 
8861   /// Add all reduction operations for the reduction instruction \p I.
8862   void addReductionOps(Instruction *I) {
8863     if (isCmpSelMinMax(I)) {
8864       ReductionOps[0].emplace_back(cast<SelectInst>(I)->getCondition());
8865       ReductionOps[1].emplace_back(I);
8866     } else {
8867       ReductionOps[0].emplace_back(I);
8868     }
8869   }
8870 
8871   static Value *getLHS(RecurKind Kind, Instruction *I) {
8872     if (Kind == RecurKind::None)
8873       return nullptr;
8874     return I->getOperand(getFirstOperandIndex(I));
8875   }
8876   static Value *getRHS(RecurKind Kind, Instruction *I) {
8877     if (Kind == RecurKind::None)
8878       return nullptr;
8879     return I->getOperand(getFirstOperandIndex(I) + 1);
8880   }
8881 
8882 public:
8883   HorizontalReduction() = default;
8884 
8885   /// Try to find a reduction tree.
8886   bool matchAssociativeReduction(PHINode *Phi, Instruction *Inst) {
8887     assert((!Phi || is_contained(Phi->operands(), Inst)) &&
8888            "Phi needs to use the binary operator");
8889     assert((isa<BinaryOperator>(Inst) || isa<SelectInst>(Inst) ||
8890             isa<IntrinsicInst>(Inst)) &&
8891            "Expected binop, select, or intrinsic for reduction matching");
8892     RdxKind = getRdxKind(Inst);
8893 
8894     // We could have a initial reductions that is not an add.
8895     //  r *= v1 + v2 + v3 + v4
8896     // In such a case start looking for a tree rooted in the first '+'.
8897     if (Phi) {
8898       if (getLHS(RdxKind, Inst) == Phi) {
8899         Phi = nullptr;
8900         Inst = dyn_cast<Instruction>(getRHS(RdxKind, Inst));
8901         if (!Inst)
8902           return false;
8903         RdxKind = getRdxKind(Inst);
8904       } else if (getRHS(RdxKind, Inst) == Phi) {
8905         Phi = nullptr;
8906         Inst = dyn_cast<Instruction>(getLHS(RdxKind, Inst));
8907         if (!Inst)
8908           return false;
8909         RdxKind = getRdxKind(Inst);
8910       }
8911     }
8912 
8913     if (!isVectorizable(RdxKind, Inst))
8914       return false;
8915 
8916     // Analyze "regular" integer/FP types for reductions - no target-specific
8917     // types or pointers.
8918     Type *Ty = Inst->getType();
8919     if (!isValidElementType(Ty) || Ty->isPointerTy())
8920       return false;
8921 
8922     // Though the ultimate reduction may have multiple uses, its condition must
8923     // have only single use.
8924     if (auto *Sel = dyn_cast<SelectInst>(Inst))
8925       if (!Sel->getCondition()->hasOneUse())
8926         return false;
8927 
8928     ReductionRoot = Inst;
8929 
8930     // The opcode for leaf values that we perform a reduction on.
8931     // For example: load(x) + load(y) + load(z) + fptoui(w)
8932     // The leaf opcode for 'w' does not match, so we don't include it as a
8933     // potential candidate for the reduction.
8934     unsigned LeafOpcode = 0;
8935 
8936     // Post-order traverse the reduction tree starting at Inst. We only handle
8937     // true trees containing binary operators or selects.
8938     SmallVector<std::pair<Instruction *, unsigned>, 32> Stack;
8939     Stack.push_back(std::make_pair(Inst, getFirstOperandIndex(Inst)));
8940     initReductionOps(Inst);
8941     while (!Stack.empty()) {
8942       Instruction *TreeN = Stack.back().first;
8943       unsigned EdgeToVisit = Stack.back().second++;
8944       const RecurKind TreeRdxKind = getRdxKind(TreeN);
8945       bool IsReducedValue = TreeRdxKind != RdxKind;
8946 
8947       // Postorder visit.
8948       if (IsReducedValue || EdgeToVisit >= getNumberOfOperands(TreeN)) {
8949         if (IsReducedValue)
8950           ReducedVals.push_back(TreeN);
8951         else {
8952           auto ExtraArgsIter = ExtraArgs.find(TreeN);
8953           if (ExtraArgsIter != ExtraArgs.end() && !ExtraArgsIter->second) {
8954             // Check if TreeN is an extra argument of its parent operation.
8955             if (Stack.size() <= 1) {
8956               // TreeN can't be an extra argument as it is a root reduction
8957               // operation.
8958               return false;
8959             }
8960             // Yes, TreeN is an extra argument, do not add it to a list of
8961             // reduction operations.
8962             // Stack[Stack.size() - 2] always points to the parent operation.
8963             markExtraArg(Stack[Stack.size() - 2], TreeN);
8964             ExtraArgs.erase(TreeN);
8965           } else
8966             addReductionOps(TreeN);
8967         }
8968         // Retract.
8969         Stack.pop_back();
8970         continue;
8971       }
8972 
8973       // Visit operands.
8974       Value *EdgeVal = getRdxOperand(TreeN, EdgeToVisit);
8975       auto *EdgeInst = dyn_cast<Instruction>(EdgeVal);
8976       if (!EdgeInst) {
8977         // Edge value is not a reduction instruction or a leaf instruction.
8978         // (It may be a constant, function argument, or something else.)
8979         markExtraArg(Stack.back(), EdgeVal);
8980         continue;
8981       }
8982       RecurKind EdgeRdxKind = getRdxKind(EdgeInst);
8983       // Continue analysis if the next operand is a reduction operation or
8984       // (possibly) a leaf value. If the leaf value opcode is not set,
8985       // the first met operation != reduction operation is considered as the
8986       // leaf opcode.
8987       // Only handle trees in the current basic block.
8988       // Each tree node needs to have minimal number of users except for the
8989       // ultimate reduction.
8990       const bool IsRdxInst = EdgeRdxKind == RdxKind;
8991       if (EdgeInst != Phi && EdgeInst != Inst &&
8992           hasSameParent(EdgeInst, Inst->getParent()) &&
8993           hasRequiredNumberOfUses(isCmpSelMinMax(Inst), EdgeInst) &&
8994           (!LeafOpcode || LeafOpcode == EdgeInst->getOpcode() || IsRdxInst)) {
8995         if (IsRdxInst) {
8996           // We need to be able to reassociate the reduction operations.
8997           if (!isVectorizable(EdgeRdxKind, EdgeInst)) {
8998             // I is an extra argument for TreeN (its parent operation).
8999             markExtraArg(Stack.back(), EdgeInst);
9000             continue;
9001           }
9002         } else if (!LeafOpcode) {
9003           LeafOpcode = EdgeInst->getOpcode();
9004         }
9005         Stack.push_back(
9006             std::make_pair(EdgeInst, getFirstOperandIndex(EdgeInst)));
9007         continue;
9008       }
9009       // I is an extra argument for TreeN (its parent operation).
9010       markExtraArg(Stack.back(), EdgeInst);
9011     }
9012     return true;
9013   }
9014 
9015   /// Attempt to vectorize the tree found by matchAssociativeReduction.
9016   Value *tryToReduce(BoUpSLP &V, TargetTransformInfo *TTI) {
9017     // If there are a sufficient number of reduction values, reduce
9018     // to a nearby power-of-2. We can safely generate oversized
9019     // vectors and rely on the backend to split them to legal sizes.
9020     unsigned NumReducedVals = ReducedVals.size();
9021     if (NumReducedVals < 4)
9022       return nullptr;
9023 
9024     // Intersect the fast-math-flags from all reduction operations.
9025     FastMathFlags RdxFMF;
9026     RdxFMF.set();
9027     for (ReductionOpsType &RdxOp : ReductionOps) {
9028       for (Value *RdxVal : RdxOp) {
9029         if (auto *FPMO = dyn_cast<FPMathOperator>(RdxVal))
9030           RdxFMF &= FPMO->getFastMathFlags();
9031       }
9032     }
9033 
9034     IRBuilder<> Builder(cast<Instruction>(ReductionRoot));
9035     Builder.setFastMathFlags(RdxFMF);
9036 
9037     BoUpSLP::ExtraValueToDebugLocsMap ExternallyUsedValues;
9038     // The same extra argument may be used several times, so log each attempt
9039     // to use it.
9040     for (const std::pair<Instruction *, Value *> &Pair : ExtraArgs) {
9041       assert(Pair.first && "DebugLoc must be set.");
9042       ExternallyUsedValues[Pair.second].push_back(Pair.first);
9043     }
9044 
9045     // The compare instruction of a min/max is the insertion point for new
9046     // instructions and may be replaced with a new compare instruction.
9047     auto getCmpForMinMaxReduction = [](Instruction *RdxRootInst) {
9048       assert(isa<SelectInst>(RdxRootInst) &&
9049              "Expected min/max reduction to have select root instruction");
9050       Value *ScalarCond = cast<SelectInst>(RdxRootInst)->getCondition();
9051       assert(isa<Instruction>(ScalarCond) &&
9052              "Expected min/max reduction to have compare condition");
9053       return cast<Instruction>(ScalarCond);
9054     };
9055 
9056     // The reduction root is used as the insertion point for new instructions,
9057     // so set it as externally used to prevent it from being deleted.
9058     ExternallyUsedValues[ReductionRoot];
9059     SmallVector<Value *, 16> IgnoreList;
9060     for (ReductionOpsType &RdxOp : ReductionOps)
9061       IgnoreList.append(RdxOp.begin(), RdxOp.end());
9062 
9063     unsigned ReduxWidth = PowerOf2Floor(NumReducedVals);
9064     if (NumReducedVals > ReduxWidth) {
9065       // In the loop below, we are building a tree based on a window of
9066       // 'ReduxWidth' values.
9067       // If the operands of those values have common traits (compare predicate,
9068       // constant operand, etc), then we want to group those together to
9069       // minimize the cost of the reduction.
9070 
9071       // TODO: This should be extended to count common operands for
9072       //       compares and binops.
9073 
9074       // Step 1: Count the number of times each compare predicate occurs.
9075       SmallDenseMap<unsigned, unsigned> PredCountMap;
9076       for (Value *RdxVal : ReducedVals) {
9077         CmpInst::Predicate Pred;
9078         if (match(RdxVal, m_Cmp(Pred, m_Value(), m_Value())))
9079           ++PredCountMap[Pred];
9080       }
9081       // Step 2: Sort the values so the most common predicates come first.
9082       stable_sort(ReducedVals, [&PredCountMap](Value *A, Value *B) {
9083         CmpInst::Predicate PredA, PredB;
9084         if (match(A, m_Cmp(PredA, m_Value(), m_Value())) &&
9085             match(B, m_Cmp(PredB, m_Value(), m_Value()))) {
9086           return PredCountMap[PredA] > PredCountMap[PredB];
9087         }
9088         return false;
9089       });
9090     }
9091 
9092     Value *VectorizedTree = nullptr;
9093     unsigned i = 0;
9094     while (i < NumReducedVals - ReduxWidth + 1 && ReduxWidth > 2) {
9095       ArrayRef<Value *> VL(&ReducedVals[i], ReduxWidth);
9096       V.buildTree(VL, IgnoreList);
9097       if (V.isTreeTinyAndNotFullyVectorizable(/*ForReduction=*/true))
9098         break;
9099       if (V.isLoadCombineReductionCandidate(RdxKind))
9100         break;
9101       V.reorderTopToBottom();
9102       V.reorderBottomToTop(/*IgnoreReorder=*/true);
9103       V.buildExternalUses(ExternallyUsedValues);
9104 
9105       // For a poison-safe boolean logic reduction, do not replace select
9106       // instructions with logic ops. All reduced values will be frozen (see
9107       // below) to prevent leaking poison.
9108       if (isa<SelectInst>(ReductionRoot) &&
9109           isBoolLogicOp(cast<Instruction>(ReductionRoot)) &&
9110           NumReducedVals != ReduxWidth)
9111         break;
9112 
9113       V.computeMinimumValueSizes();
9114 
9115       // Estimate cost.
9116       InstructionCost TreeCost =
9117           V.getTreeCost(makeArrayRef(&ReducedVals[i], ReduxWidth));
9118       InstructionCost ReductionCost =
9119           getReductionCost(TTI, ReducedVals[i], ReduxWidth, RdxFMF);
9120       InstructionCost Cost = TreeCost + ReductionCost;
9121       if (!Cost.isValid()) {
9122         LLVM_DEBUG(dbgs() << "Encountered invalid baseline cost.\n");
9123         return nullptr;
9124       }
9125       if (Cost >= -SLPCostThreshold) {
9126         V.getORE()->emit([&]() {
9127           return OptimizationRemarkMissed(SV_NAME, "HorSLPNotBeneficial",
9128                                           cast<Instruction>(VL[0]))
9129                  << "Vectorizing horizontal reduction is possible"
9130                  << "but not beneficial with cost " << ore::NV("Cost", Cost)
9131                  << " and threshold "
9132                  << ore::NV("Threshold", -SLPCostThreshold);
9133         });
9134         break;
9135       }
9136 
9137       LLVM_DEBUG(dbgs() << "SLP: Vectorizing horizontal reduction at cost:"
9138                         << Cost << ". (HorRdx)\n");
9139       V.getORE()->emit([&]() {
9140         return OptimizationRemark(SV_NAME, "VectorizedHorizontalReduction",
9141                                   cast<Instruction>(VL[0]))
9142                << "Vectorized horizontal reduction with cost "
9143                << ore::NV("Cost", Cost) << " and with tree size "
9144                << ore::NV("TreeSize", V.getTreeSize());
9145       });
9146 
9147       // Vectorize a tree.
9148       DebugLoc Loc = cast<Instruction>(ReducedVals[i])->getDebugLoc();
9149       Value *VectorizedRoot = V.vectorizeTree(ExternallyUsedValues);
9150 
9151       // Emit a reduction. If the root is a select (min/max idiom), the insert
9152       // point is the compare condition of that select.
9153       Instruction *RdxRootInst = cast<Instruction>(ReductionRoot);
9154       if (isCmpSelMinMax(RdxRootInst))
9155         Builder.SetInsertPoint(getCmpForMinMaxReduction(RdxRootInst));
9156       else
9157         Builder.SetInsertPoint(RdxRootInst);
9158 
9159       // To prevent poison from leaking across what used to be sequential, safe,
9160       // scalar boolean logic operations, the reduction operand must be frozen.
9161       if (isa<SelectInst>(RdxRootInst) && isBoolLogicOp(RdxRootInst))
9162         VectorizedRoot = Builder.CreateFreeze(VectorizedRoot);
9163 
9164       Value *ReducedSubTree =
9165           emitReduction(VectorizedRoot, Builder, ReduxWidth, TTI);
9166 
9167       if (!VectorizedTree) {
9168         // Initialize the final value in the reduction.
9169         VectorizedTree = ReducedSubTree;
9170       } else {
9171         // Update the final value in the reduction.
9172         Builder.SetCurrentDebugLocation(Loc);
9173         VectorizedTree = createOp(Builder, RdxKind, VectorizedTree,
9174                                   ReducedSubTree, "op.rdx", ReductionOps);
9175       }
9176       i += ReduxWidth;
9177       ReduxWidth = PowerOf2Floor(NumReducedVals - i);
9178     }
9179 
9180     if (VectorizedTree) {
9181       // Finish the reduction.
9182       for (; i < NumReducedVals; ++i) {
9183         auto *I = cast<Instruction>(ReducedVals[i]);
9184         Builder.SetCurrentDebugLocation(I->getDebugLoc());
9185         VectorizedTree =
9186             createOp(Builder, RdxKind, VectorizedTree, I, "", ReductionOps);
9187       }
9188       for (auto &Pair : ExternallyUsedValues) {
9189         // Add each externally used value to the final reduction.
9190         for (auto *I : Pair.second) {
9191           Builder.SetCurrentDebugLocation(I->getDebugLoc());
9192           VectorizedTree = createOp(Builder, RdxKind, VectorizedTree,
9193                                     Pair.first, "op.extra", I);
9194         }
9195       }
9196 
9197       ReductionRoot->replaceAllUsesWith(VectorizedTree);
9198 
9199       // Mark all scalar reduction ops for deletion, they are replaced by the
9200       // vector reductions.
9201       V.eraseInstructions(IgnoreList);
9202     }
9203     return VectorizedTree;
9204   }
9205 
9206   unsigned numReductionValues() const { return ReducedVals.size(); }
9207 
9208 private:
9209   /// Calculate the cost of a reduction.
9210   InstructionCost getReductionCost(TargetTransformInfo *TTI,
9211                                    Value *FirstReducedVal, unsigned ReduxWidth,
9212                                    FastMathFlags FMF) {
9213     TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
9214     Type *ScalarTy = FirstReducedVal->getType();
9215     FixedVectorType *VectorTy = FixedVectorType::get(ScalarTy, ReduxWidth);
9216     InstructionCost VectorCost, ScalarCost;
9217     switch (RdxKind) {
9218     case RecurKind::Add:
9219     case RecurKind::Mul:
9220     case RecurKind::Or:
9221     case RecurKind::And:
9222     case RecurKind::Xor:
9223     case RecurKind::FAdd:
9224     case RecurKind::FMul: {
9225       unsigned RdxOpcode = RecurrenceDescriptor::getOpcode(RdxKind);
9226       VectorCost =
9227           TTI->getArithmeticReductionCost(RdxOpcode, VectorTy, FMF, CostKind);
9228       ScalarCost = TTI->getArithmeticInstrCost(RdxOpcode, ScalarTy, CostKind);
9229       break;
9230     }
9231     case RecurKind::FMax:
9232     case RecurKind::FMin: {
9233       auto *SclCondTy = CmpInst::makeCmpResultType(ScalarTy);
9234       auto *VecCondTy = cast<VectorType>(CmpInst::makeCmpResultType(VectorTy));
9235       VectorCost = TTI->getMinMaxReductionCost(VectorTy, VecCondTy,
9236                                                /*IsUnsigned=*/false, CostKind);
9237       CmpInst::Predicate RdxPred = getMinMaxReductionPredicate(RdxKind);
9238       ScalarCost = TTI->getCmpSelInstrCost(Instruction::FCmp, ScalarTy,
9239                                            SclCondTy, RdxPred, CostKind) +
9240                    TTI->getCmpSelInstrCost(Instruction::Select, ScalarTy,
9241                                            SclCondTy, RdxPred, CostKind);
9242       break;
9243     }
9244     case RecurKind::SMax:
9245     case RecurKind::SMin:
9246     case RecurKind::UMax:
9247     case RecurKind::UMin: {
9248       auto *SclCondTy = CmpInst::makeCmpResultType(ScalarTy);
9249       auto *VecCondTy = cast<VectorType>(CmpInst::makeCmpResultType(VectorTy));
9250       bool IsUnsigned =
9251           RdxKind == RecurKind::UMax || RdxKind == RecurKind::UMin;
9252       VectorCost = TTI->getMinMaxReductionCost(VectorTy, VecCondTy, IsUnsigned,
9253                                                CostKind);
9254       CmpInst::Predicate RdxPred = getMinMaxReductionPredicate(RdxKind);
9255       ScalarCost = TTI->getCmpSelInstrCost(Instruction::ICmp, ScalarTy,
9256                                            SclCondTy, RdxPred, CostKind) +
9257                    TTI->getCmpSelInstrCost(Instruction::Select, ScalarTy,
9258                                            SclCondTy, RdxPred, CostKind);
9259       break;
9260     }
9261     default:
9262       llvm_unreachable("Expected arithmetic or min/max reduction operation");
9263     }
9264 
9265     // Scalar cost is repeated for N-1 elements.
9266     ScalarCost *= (ReduxWidth - 1);
9267     LLVM_DEBUG(dbgs() << "SLP: Adding cost " << VectorCost - ScalarCost
9268                       << " for reduction that starts with " << *FirstReducedVal
9269                       << " (It is a splitting reduction)\n");
9270     return VectorCost - ScalarCost;
9271   }
9272 
9273   /// Emit a horizontal reduction of the vectorized value.
9274   Value *emitReduction(Value *VectorizedValue, IRBuilder<> &Builder,
9275                        unsigned ReduxWidth, const TargetTransformInfo *TTI) {
9276     assert(VectorizedValue && "Need to have a vectorized tree node");
9277     assert(isPowerOf2_32(ReduxWidth) &&
9278            "We only handle power-of-two reductions for now");
9279     assert(RdxKind != RecurKind::FMulAdd &&
9280            "A call to the llvm.fmuladd intrinsic is not handled yet");
9281 
9282     ++NumVectorInstructions;
9283     return createSimpleTargetReduction(Builder, TTI, VectorizedValue, RdxKind);
9284   }
9285 };
9286 
9287 } // end anonymous namespace
9288 
9289 static Optional<unsigned> getAggregateSize(Instruction *InsertInst) {
9290   if (auto *IE = dyn_cast<InsertElementInst>(InsertInst))
9291     return cast<FixedVectorType>(IE->getType())->getNumElements();
9292 
9293   unsigned AggregateSize = 1;
9294   auto *IV = cast<InsertValueInst>(InsertInst);
9295   Type *CurrentType = IV->getType();
9296   do {
9297     if (auto *ST = dyn_cast<StructType>(CurrentType)) {
9298       for (auto *Elt : ST->elements())
9299         if (Elt != ST->getElementType(0)) // check homogeneity
9300           return None;
9301       AggregateSize *= ST->getNumElements();
9302       CurrentType = ST->getElementType(0);
9303     } else if (auto *AT = dyn_cast<ArrayType>(CurrentType)) {
9304       AggregateSize *= AT->getNumElements();
9305       CurrentType = AT->getElementType();
9306     } else if (auto *VT = dyn_cast<FixedVectorType>(CurrentType)) {
9307       AggregateSize *= VT->getNumElements();
9308       return AggregateSize;
9309     } else if (CurrentType->isSingleValueType()) {
9310       return AggregateSize;
9311     } else {
9312       return None;
9313     }
9314   } while (true);
9315 }
9316 
9317 static bool findBuildAggregate_rec(Instruction *LastInsertInst,
9318                                    TargetTransformInfo *TTI,
9319                                    SmallVectorImpl<Value *> &BuildVectorOpds,
9320                                    SmallVectorImpl<Value *> &InsertElts,
9321                                    unsigned OperandOffset) {
9322   do {
9323     Value *InsertedOperand = LastInsertInst->getOperand(1);
9324     Optional<int> OperandIndex = getInsertIndex(LastInsertInst, OperandOffset);
9325     if (!OperandIndex)
9326       return false;
9327     if (isa<InsertElementInst>(InsertedOperand) ||
9328         isa<InsertValueInst>(InsertedOperand)) {
9329       if (!findBuildAggregate_rec(cast<Instruction>(InsertedOperand), TTI,
9330                                   BuildVectorOpds, InsertElts, *OperandIndex))
9331         return false;
9332     } else {
9333       BuildVectorOpds[*OperandIndex] = InsertedOperand;
9334       InsertElts[*OperandIndex] = LastInsertInst;
9335     }
9336     LastInsertInst = dyn_cast<Instruction>(LastInsertInst->getOperand(0));
9337   } while (LastInsertInst != nullptr &&
9338            (isa<InsertValueInst>(LastInsertInst) ||
9339             isa<InsertElementInst>(LastInsertInst)) &&
9340            LastInsertInst->hasOneUse());
9341   return true;
9342 }
9343 
9344 /// Recognize construction of vectors like
9345 ///  %ra = insertelement <4 x float> poison, float %s0, i32 0
9346 ///  %rb = insertelement <4 x float> %ra, float %s1, i32 1
9347 ///  %rc = insertelement <4 x float> %rb, float %s2, i32 2
9348 ///  %rd = insertelement <4 x float> %rc, float %s3, i32 3
9349 ///  starting from the last insertelement or insertvalue instruction.
9350 ///
9351 /// Also recognize homogeneous aggregates like {<2 x float>, <2 x float>},
9352 /// {{float, float}, {float, float}}, [2 x {float, float}] and so on.
9353 /// See llvm/test/Transforms/SLPVectorizer/X86/pr42022.ll for examples.
9354 ///
9355 /// Assume LastInsertInst is of InsertElementInst or InsertValueInst type.
9356 ///
9357 /// \return true if it matches.
9358 static bool findBuildAggregate(Instruction *LastInsertInst,
9359                                TargetTransformInfo *TTI,
9360                                SmallVectorImpl<Value *> &BuildVectorOpds,
9361                                SmallVectorImpl<Value *> &InsertElts) {
9362 
9363   assert((isa<InsertElementInst>(LastInsertInst) ||
9364           isa<InsertValueInst>(LastInsertInst)) &&
9365          "Expected insertelement or insertvalue instruction!");
9366 
9367   assert((BuildVectorOpds.empty() && InsertElts.empty()) &&
9368          "Expected empty result vectors!");
9369 
9370   Optional<unsigned> AggregateSize = getAggregateSize(LastInsertInst);
9371   if (!AggregateSize)
9372     return false;
9373   BuildVectorOpds.resize(*AggregateSize);
9374   InsertElts.resize(*AggregateSize);
9375 
9376   if (findBuildAggregate_rec(LastInsertInst, TTI, BuildVectorOpds, InsertElts,
9377                              0)) {
9378     llvm::erase_value(BuildVectorOpds, nullptr);
9379     llvm::erase_value(InsertElts, nullptr);
9380     if (BuildVectorOpds.size() >= 2)
9381       return true;
9382   }
9383 
9384   return false;
9385 }
9386 
9387 /// Try and get a reduction value from a phi node.
9388 ///
9389 /// Given a phi node \p P in a block \p ParentBB, consider possible reductions
9390 /// if they come from either \p ParentBB or a containing loop latch.
9391 ///
9392 /// \returns A candidate reduction value if possible, or \code nullptr \endcode
9393 /// if not possible.
9394 static Value *getReductionValue(const DominatorTree *DT, PHINode *P,
9395                                 BasicBlock *ParentBB, LoopInfo *LI) {
9396   // There are situations where the reduction value is not dominated by the
9397   // reduction phi. Vectorizing such cases has been reported to cause
9398   // miscompiles. See PR25787.
9399   auto DominatedReduxValue = [&](Value *R) {
9400     return isa<Instruction>(R) &&
9401            DT->dominates(P->getParent(), cast<Instruction>(R)->getParent());
9402   };
9403 
9404   Value *Rdx = nullptr;
9405 
9406   // Return the incoming value if it comes from the same BB as the phi node.
9407   if (P->getIncomingBlock(0) == ParentBB) {
9408     Rdx = P->getIncomingValue(0);
9409   } else if (P->getIncomingBlock(1) == ParentBB) {
9410     Rdx = P->getIncomingValue(1);
9411   }
9412 
9413   if (Rdx && DominatedReduxValue(Rdx))
9414     return Rdx;
9415 
9416   // Otherwise, check whether we have a loop latch to look at.
9417   Loop *BBL = LI->getLoopFor(ParentBB);
9418   if (!BBL)
9419     return nullptr;
9420   BasicBlock *BBLatch = BBL->getLoopLatch();
9421   if (!BBLatch)
9422     return nullptr;
9423 
9424   // There is a loop latch, return the incoming value if it comes from
9425   // that. This reduction pattern occasionally turns up.
9426   if (P->getIncomingBlock(0) == BBLatch) {
9427     Rdx = P->getIncomingValue(0);
9428   } else if (P->getIncomingBlock(1) == BBLatch) {
9429     Rdx = P->getIncomingValue(1);
9430   }
9431 
9432   if (Rdx && DominatedReduxValue(Rdx))
9433     return Rdx;
9434 
9435   return nullptr;
9436 }
9437 
9438 static bool matchRdxBop(Instruction *I, Value *&V0, Value *&V1) {
9439   if (match(I, m_BinOp(m_Value(V0), m_Value(V1))))
9440     return true;
9441   if (match(I, m_Intrinsic<Intrinsic::maxnum>(m_Value(V0), m_Value(V1))))
9442     return true;
9443   if (match(I, m_Intrinsic<Intrinsic::minnum>(m_Value(V0), m_Value(V1))))
9444     return true;
9445   if (match(I, m_Intrinsic<Intrinsic::smax>(m_Value(V0), m_Value(V1))))
9446     return true;
9447   if (match(I, m_Intrinsic<Intrinsic::smin>(m_Value(V0), m_Value(V1))))
9448     return true;
9449   if (match(I, m_Intrinsic<Intrinsic::umax>(m_Value(V0), m_Value(V1))))
9450     return true;
9451   if (match(I, m_Intrinsic<Intrinsic::umin>(m_Value(V0), m_Value(V1))))
9452     return true;
9453   return false;
9454 }
9455 
9456 /// Attempt to reduce a horizontal reduction.
9457 /// If it is legal to match a horizontal reduction feeding the phi node \a P
9458 /// with reduction operators \a Root (or one of its operands) in a basic block
9459 /// \a BB, then check if it can be done. If horizontal reduction is not found
9460 /// and root instruction is a binary operation, vectorization of the operands is
9461 /// attempted.
9462 /// \returns true if a horizontal reduction was matched and reduced or operands
9463 /// of one of the binary instruction were vectorized.
9464 /// \returns false if a horizontal reduction was not matched (or not possible)
9465 /// or no vectorization of any binary operation feeding \a Root instruction was
9466 /// performed.
9467 static bool tryToVectorizeHorReductionOrInstOperands(
9468     PHINode *P, Instruction *Root, BasicBlock *BB, BoUpSLP &R,
9469     TargetTransformInfo *TTI,
9470     const function_ref<bool(Instruction *, BoUpSLP &)> Vectorize) {
9471   if (!ShouldVectorizeHor)
9472     return false;
9473 
9474   if (!Root)
9475     return false;
9476 
9477   if (Root->getParent() != BB || isa<PHINode>(Root))
9478     return false;
9479   // Start analysis starting from Root instruction. If horizontal reduction is
9480   // found, try to vectorize it. If it is not a horizontal reduction or
9481   // vectorization is not possible or not effective, and currently analyzed
9482   // instruction is a binary operation, try to vectorize the operands, using
9483   // pre-order DFS traversal order. If the operands were not vectorized, repeat
9484   // the same procedure considering each operand as a possible root of the
9485   // horizontal reduction.
9486   // Interrupt the process if the Root instruction itself was vectorized or all
9487   // sub-trees not higher that RecursionMaxDepth were analyzed/vectorized.
9488   // Skip the analysis of CmpInsts.Compiler implements postanalysis of the
9489   // CmpInsts so we can skip extra attempts in
9490   // tryToVectorizeHorReductionOrInstOperands and save compile time.
9491   std::queue<std::pair<Instruction *, unsigned>> Stack;
9492   Stack.emplace(Root, 0);
9493   SmallPtrSet<Value *, 8> VisitedInstrs;
9494   SmallVector<WeakTrackingVH> PostponedInsts;
9495   bool Res = false;
9496   auto &&TryToReduce = [TTI, &P, &R](Instruction *Inst, Value *&B0,
9497                                      Value *&B1) -> Value * {
9498     bool IsBinop = matchRdxBop(Inst, B0, B1);
9499     bool IsSelect = match(Inst, m_Select(m_Value(), m_Value(), m_Value()));
9500     if (IsBinop || IsSelect) {
9501       HorizontalReduction HorRdx;
9502       if (HorRdx.matchAssociativeReduction(P, Inst))
9503         return HorRdx.tryToReduce(R, TTI);
9504     }
9505     return nullptr;
9506   };
9507   while (!Stack.empty()) {
9508     Instruction *Inst;
9509     unsigned Level;
9510     std::tie(Inst, Level) = Stack.front();
9511     Stack.pop();
9512     // Do not try to analyze instruction that has already been vectorized.
9513     // This may happen when we vectorize instruction operands on a previous
9514     // iteration while stack was populated before that happened.
9515     if (R.isDeleted(Inst))
9516       continue;
9517     Value *B0 = nullptr, *B1 = nullptr;
9518     if (Value *V = TryToReduce(Inst, B0, B1)) {
9519       Res = true;
9520       // Set P to nullptr to avoid re-analysis of phi node in
9521       // matchAssociativeReduction function unless this is the root node.
9522       P = nullptr;
9523       if (auto *I = dyn_cast<Instruction>(V)) {
9524         // Try to find another reduction.
9525         Stack.emplace(I, Level);
9526         continue;
9527       }
9528     } else {
9529       bool IsBinop = B0 && B1;
9530       if (P && IsBinop) {
9531         Inst = dyn_cast<Instruction>(B0);
9532         if (Inst == P)
9533           Inst = dyn_cast<Instruction>(B1);
9534         if (!Inst) {
9535           // Set P to nullptr to avoid re-analysis of phi node in
9536           // matchAssociativeReduction function unless this is the root node.
9537           P = nullptr;
9538           continue;
9539         }
9540       }
9541       // Set P to nullptr to avoid re-analysis of phi node in
9542       // matchAssociativeReduction function unless this is the root node.
9543       P = nullptr;
9544       // Do not try to vectorize CmpInst operands, this is done separately.
9545       // Final attempt for binop args vectorization should happen after the loop
9546       // to try to find reductions.
9547       if (!isa<CmpInst>(Inst))
9548         PostponedInsts.push_back(Inst);
9549     }
9550 
9551     // Try to vectorize operands.
9552     // Continue analysis for the instruction from the same basic block only to
9553     // save compile time.
9554     if (++Level < RecursionMaxDepth)
9555       for (auto *Op : Inst->operand_values())
9556         if (VisitedInstrs.insert(Op).second)
9557           if (auto *I = dyn_cast<Instruction>(Op))
9558             // Do not try to vectorize CmpInst operands,  this is done
9559             // separately.
9560             if (!isa<PHINode>(I) && !isa<CmpInst>(I) && !R.isDeleted(I) &&
9561                 I->getParent() == BB)
9562               Stack.emplace(I, Level);
9563   }
9564   // Try to vectorized binops where reductions were not found.
9565   for (Value *V : PostponedInsts)
9566     if (auto *Inst = dyn_cast<Instruction>(V))
9567       if (!R.isDeleted(Inst))
9568         Res |= Vectorize(Inst, R);
9569   return Res;
9570 }
9571 
9572 bool SLPVectorizerPass::vectorizeRootInstruction(PHINode *P, Value *V,
9573                                                  BasicBlock *BB, BoUpSLP &R,
9574                                                  TargetTransformInfo *TTI) {
9575   auto *I = dyn_cast_or_null<Instruction>(V);
9576   if (!I)
9577     return false;
9578 
9579   if (!isa<BinaryOperator>(I))
9580     P = nullptr;
9581   // Try to match and vectorize a horizontal reduction.
9582   auto &&ExtraVectorization = [this](Instruction *I, BoUpSLP &R) -> bool {
9583     return tryToVectorize(I, R);
9584   };
9585   return tryToVectorizeHorReductionOrInstOperands(P, I, BB, R, TTI,
9586                                                   ExtraVectorization);
9587 }
9588 
9589 bool SLPVectorizerPass::vectorizeInsertValueInst(InsertValueInst *IVI,
9590                                                  BasicBlock *BB, BoUpSLP &R) {
9591   const DataLayout &DL = BB->getModule()->getDataLayout();
9592   if (!R.canMapToVector(IVI->getType(), DL))
9593     return false;
9594 
9595   SmallVector<Value *, 16> BuildVectorOpds;
9596   SmallVector<Value *, 16> BuildVectorInsts;
9597   if (!findBuildAggregate(IVI, TTI, BuildVectorOpds, BuildVectorInsts))
9598     return false;
9599 
9600   LLVM_DEBUG(dbgs() << "SLP: array mappable to vector: " << *IVI << "\n");
9601   // Aggregate value is unlikely to be processed in vector register.
9602   return tryToVectorizeList(BuildVectorOpds, R);
9603 }
9604 
9605 bool SLPVectorizerPass::vectorizeInsertElementInst(InsertElementInst *IEI,
9606                                                    BasicBlock *BB, BoUpSLP &R) {
9607   SmallVector<Value *, 16> BuildVectorInsts;
9608   SmallVector<Value *, 16> BuildVectorOpds;
9609   SmallVector<int> Mask;
9610   if (!findBuildAggregate(IEI, TTI, BuildVectorOpds, BuildVectorInsts) ||
9611       (llvm::all_of(
9612            BuildVectorOpds,
9613            [](Value *V) { return isa<ExtractElementInst, UndefValue>(V); }) &&
9614        isFixedVectorShuffle(BuildVectorOpds, Mask)))
9615     return false;
9616 
9617   LLVM_DEBUG(dbgs() << "SLP: array mappable to vector: " << *IEI << "\n");
9618   return tryToVectorizeList(BuildVectorInsts, R);
9619 }
9620 
9621 template <typename T>
9622 static bool
9623 tryToVectorizeSequence(SmallVectorImpl<T *> &Incoming,
9624                        function_ref<unsigned(T *)> Limit,
9625                        function_ref<bool(T *, T *)> Comparator,
9626                        function_ref<bool(T *, T *)> AreCompatible,
9627                        function_ref<bool(ArrayRef<T *>, bool)> TryToVectorize,
9628                        bool LimitForRegisterSize) {
9629   bool Changed = false;
9630   // Sort by type, parent, operands.
9631   stable_sort(Incoming, Comparator);
9632 
9633   // Try to vectorize elements base on their type.
9634   SmallVector<T *> Candidates;
9635   for (auto *IncIt = Incoming.begin(), *E = Incoming.end(); IncIt != E;) {
9636     // Look for the next elements with the same type, parent and operand
9637     // kinds.
9638     auto *SameTypeIt = IncIt;
9639     while (SameTypeIt != E && AreCompatible(*SameTypeIt, *IncIt))
9640       ++SameTypeIt;
9641 
9642     // Try to vectorize them.
9643     unsigned NumElts = (SameTypeIt - IncIt);
9644     LLVM_DEBUG(dbgs() << "SLP: Trying to vectorize starting at nodes ("
9645                       << NumElts << ")\n");
9646     // The vectorization is a 3-state attempt:
9647     // 1. Try to vectorize instructions with the same/alternate opcodes with the
9648     // size of maximal register at first.
9649     // 2. Try to vectorize remaining instructions with the same type, if
9650     // possible. This may result in the better vectorization results rather than
9651     // if we try just to vectorize instructions with the same/alternate opcodes.
9652     // 3. Final attempt to try to vectorize all instructions with the
9653     // same/alternate ops only, this may result in some extra final
9654     // vectorization.
9655     if (NumElts > 1 &&
9656         TryToVectorize(makeArrayRef(IncIt, NumElts), LimitForRegisterSize)) {
9657       // Success start over because instructions might have been changed.
9658       Changed = true;
9659     } else if (NumElts < Limit(*IncIt) &&
9660                (Candidates.empty() ||
9661                 Candidates.front()->getType() == (*IncIt)->getType())) {
9662       Candidates.append(IncIt, std::next(IncIt, NumElts));
9663     }
9664     // Final attempt to vectorize instructions with the same types.
9665     if (Candidates.size() > 1 &&
9666         (SameTypeIt == E || (*SameTypeIt)->getType() != (*IncIt)->getType())) {
9667       if (TryToVectorize(Candidates, /*LimitForRegisterSize=*/false)) {
9668         // Success start over because instructions might have been changed.
9669         Changed = true;
9670       } else if (LimitForRegisterSize) {
9671         // Try to vectorize using small vectors.
9672         for (auto *It = Candidates.begin(), *End = Candidates.end();
9673              It != End;) {
9674           auto *SameTypeIt = It;
9675           while (SameTypeIt != End && AreCompatible(*SameTypeIt, *It))
9676             ++SameTypeIt;
9677           unsigned NumElts = (SameTypeIt - It);
9678           if (NumElts > 1 && TryToVectorize(makeArrayRef(It, NumElts),
9679                                             /*LimitForRegisterSize=*/false))
9680             Changed = true;
9681           It = SameTypeIt;
9682         }
9683       }
9684       Candidates.clear();
9685     }
9686 
9687     // Start over at the next instruction of a different type (or the end).
9688     IncIt = SameTypeIt;
9689   }
9690   return Changed;
9691 }
9692 
9693 /// Compare two cmp instructions. If IsCompatibility is true, function returns
9694 /// true if 2 cmps have same/swapped predicates and mos compatible corresponding
9695 /// operands. If IsCompatibility is false, function implements strict weak
9696 /// ordering relation between two cmp instructions, returning true if the first
9697 /// instruction is "less" than the second, i.e. its predicate is less than the
9698 /// predicate of the second or the operands IDs are less than the operands IDs
9699 /// of the second cmp instruction.
9700 template <bool IsCompatibility>
9701 static bool compareCmp(Value *V, Value *V2,
9702                        function_ref<bool(Instruction *)> IsDeleted) {
9703   auto *CI1 = cast<CmpInst>(V);
9704   auto *CI2 = cast<CmpInst>(V2);
9705   if (IsDeleted(CI2) || !isValidElementType(CI2->getType()))
9706     return false;
9707   if (CI1->getOperand(0)->getType()->getTypeID() <
9708       CI2->getOperand(0)->getType()->getTypeID())
9709     return !IsCompatibility;
9710   if (CI1->getOperand(0)->getType()->getTypeID() >
9711       CI2->getOperand(0)->getType()->getTypeID())
9712     return false;
9713   CmpInst::Predicate Pred1 = CI1->getPredicate();
9714   CmpInst::Predicate Pred2 = CI2->getPredicate();
9715   CmpInst::Predicate SwapPred1 = CmpInst::getSwappedPredicate(Pred1);
9716   CmpInst::Predicate SwapPred2 = CmpInst::getSwappedPredicate(Pred2);
9717   CmpInst::Predicate BasePred1 = std::min(Pred1, SwapPred1);
9718   CmpInst::Predicate BasePred2 = std::min(Pred2, SwapPred2);
9719   if (BasePred1 < BasePred2)
9720     return !IsCompatibility;
9721   if (BasePred1 > BasePred2)
9722     return false;
9723   // Compare operands.
9724   bool LEPreds = Pred1 <= Pred2;
9725   bool GEPreds = Pred1 >= Pred2;
9726   for (int I = 0, E = CI1->getNumOperands(); I < E; ++I) {
9727     auto *Op1 = CI1->getOperand(LEPreds ? I : E - I - 1);
9728     auto *Op2 = CI2->getOperand(GEPreds ? I : E - I - 1);
9729     if (Op1->getValueID() < Op2->getValueID())
9730       return !IsCompatibility;
9731     if (Op1->getValueID() > Op2->getValueID())
9732       return false;
9733     if (auto *I1 = dyn_cast<Instruction>(Op1))
9734       if (auto *I2 = dyn_cast<Instruction>(Op2)) {
9735         if (I1->getParent() != I2->getParent())
9736           return false;
9737         InstructionsState S = getSameOpcode({I1, I2});
9738         if (S.getOpcode())
9739           continue;
9740         return false;
9741       }
9742   }
9743   return IsCompatibility;
9744 }
9745 
9746 bool SLPVectorizerPass::vectorizeSimpleInstructions(
9747     SmallVectorImpl<Instruction *> &Instructions, BasicBlock *BB, BoUpSLP &R,
9748     bool AtTerminator) {
9749   bool OpsChanged = false;
9750   SmallVector<Instruction *, 4> PostponedCmps;
9751   for (auto *I : reverse(Instructions)) {
9752     if (R.isDeleted(I))
9753       continue;
9754     if (auto *LastInsertValue = dyn_cast<InsertValueInst>(I))
9755       OpsChanged |= vectorizeInsertValueInst(LastInsertValue, BB, R);
9756     else if (auto *LastInsertElem = dyn_cast<InsertElementInst>(I))
9757       OpsChanged |= vectorizeInsertElementInst(LastInsertElem, BB, R);
9758     else if (isa<CmpInst>(I))
9759       PostponedCmps.push_back(I);
9760   }
9761   if (AtTerminator) {
9762     // Try to find reductions first.
9763     for (Instruction *I : PostponedCmps) {
9764       if (R.isDeleted(I))
9765         continue;
9766       for (Value *Op : I->operands())
9767         OpsChanged |= vectorizeRootInstruction(nullptr, Op, BB, R, TTI);
9768     }
9769     // Try to vectorize operands as vector bundles.
9770     for (Instruction *I : PostponedCmps) {
9771       if (R.isDeleted(I))
9772         continue;
9773       OpsChanged |= tryToVectorize(I, R);
9774     }
9775     // Try to vectorize list of compares.
9776     // Sort by type, compare predicate, etc.
9777     auto &&CompareSorter = [&R](Value *V, Value *V2) {
9778       return compareCmp<false>(V, V2,
9779                                [&R](Instruction *I) { return R.isDeleted(I); });
9780     };
9781 
9782     auto &&AreCompatibleCompares = [&R](Value *V1, Value *V2) {
9783       if (V1 == V2)
9784         return true;
9785       return compareCmp<true>(V1, V2,
9786                               [&R](Instruction *I) { return R.isDeleted(I); });
9787     };
9788     auto Limit = [&R](Value *V) {
9789       unsigned EltSize = R.getVectorElementSize(V);
9790       return std::max(2U, R.getMaxVecRegSize() / EltSize);
9791     };
9792 
9793     SmallVector<Value *> Vals(PostponedCmps.begin(), PostponedCmps.end());
9794     OpsChanged |= tryToVectorizeSequence<Value>(
9795         Vals, Limit, CompareSorter, AreCompatibleCompares,
9796         [this, &R](ArrayRef<Value *> Candidates, bool LimitForRegisterSize) {
9797           // Exclude possible reductions from other blocks.
9798           bool ArePossiblyReducedInOtherBlock =
9799               any_of(Candidates, [](Value *V) {
9800                 return any_of(V->users(), [V](User *U) {
9801                   return isa<SelectInst>(U) &&
9802                          cast<SelectInst>(U)->getParent() !=
9803                              cast<Instruction>(V)->getParent();
9804                 });
9805               });
9806           if (ArePossiblyReducedInOtherBlock)
9807             return false;
9808           return tryToVectorizeList(Candidates, R, LimitForRegisterSize);
9809         },
9810         /*LimitForRegisterSize=*/true);
9811     Instructions.clear();
9812   } else {
9813     // Insert in reverse order since the PostponedCmps vector was filled in
9814     // reverse order.
9815     Instructions.assign(PostponedCmps.rbegin(), PostponedCmps.rend());
9816   }
9817   return OpsChanged;
9818 }
9819 
9820 bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
9821   bool Changed = false;
9822   SmallVector<Value *, 4> Incoming;
9823   SmallPtrSet<Value *, 16> VisitedInstrs;
9824   // Maps phi nodes to the non-phi nodes found in the use tree for each phi
9825   // node. Allows better to identify the chains that can be vectorized in the
9826   // better way.
9827   DenseMap<Value *, SmallVector<Value *, 4>> PHIToOpcodes;
9828   auto PHICompare = [this, &PHIToOpcodes](Value *V1, Value *V2) {
9829     assert(isValidElementType(V1->getType()) &&
9830            isValidElementType(V2->getType()) &&
9831            "Expected vectorizable types only.");
9832     // It is fine to compare type IDs here, since we expect only vectorizable
9833     // types, like ints, floats and pointers, we don't care about other type.
9834     if (V1->getType()->getTypeID() < V2->getType()->getTypeID())
9835       return true;
9836     if (V1->getType()->getTypeID() > V2->getType()->getTypeID())
9837       return false;
9838     ArrayRef<Value *> Opcodes1 = PHIToOpcodes[V1];
9839     ArrayRef<Value *> Opcodes2 = PHIToOpcodes[V2];
9840     if (Opcodes1.size() < Opcodes2.size())
9841       return true;
9842     if (Opcodes1.size() > Opcodes2.size())
9843       return false;
9844     Optional<bool> ConstOrder;
9845     for (int I = 0, E = Opcodes1.size(); I < E; ++I) {
9846       // Undefs are compatible with any other value.
9847       if (isa<UndefValue>(Opcodes1[I]) || isa<UndefValue>(Opcodes2[I])) {
9848         if (!ConstOrder)
9849           ConstOrder =
9850               !isa<UndefValue>(Opcodes1[I]) && isa<UndefValue>(Opcodes2[I]);
9851         continue;
9852       }
9853       if (auto *I1 = dyn_cast<Instruction>(Opcodes1[I]))
9854         if (auto *I2 = dyn_cast<Instruction>(Opcodes2[I])) {
9855           DomTreeNodeBase<BasicBlock> *NodeI1 = DT->getNode(I1->getParent());
9856           DomTreeNodeBase<BasicBlock> *NodeI2 = DT->getNode(I2->getParent());
9857           if (!NodeI1)
9858             return NodeI2 != nullptr;
9859           if (!NodeI2)
9860             return false;
9861           assert((NodeI1 == NodeI2) ==
9862                      (NodeI1->getDFSNumIn() == NodeI2->getDFSNumIn()) &&
9863                  "Different nodes should have different DFS numbers");
9864           if (NodeI1 != NodeI2)
9865             return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
9866           InstructionsState S = getSameOpcode({I1, I2});
9867           if (S.getOpcode())
9868             continue;
9869           return I1->getOpcode() < I2->getOpcode();
9870         }
9871       if (isa<Constant>(Opcodes1[I]) && isa<Constant>(Opcodes2[I])) {
9872         if (!ConstOrder)
9873           ConstOrder = Opcodes1[I]->getValueID() < Opcodes2[I]->getValueID();
9874         continue;
9875       }
9876       if (Opcodes1[I]->getValueID() < Opcodes2[I]->getValueID())
9877         return true;
9878       if (Opcodes1[I]->getValueID() > Opcodes2[I]->getValueID())
9879         return false;
9880     }
9881     return ConstOrder && *ConstOrder;
9882   };
9883   auto AreCompatiblePHIs = [&PHIToOpcodes](Value *V1, Value *V2) {
9884     if (V1 == V2)
9885       return true;
9886     if (V1->getType() != V2->getType())
9887       return false;
9888     ArrayRef<Value *> Opcodes1 = PHIToOpcodes[V1];
9889     ArrayRef<Value *> Opcodes2 = PHIToOpcodes[V2];
9890     if (Opcodes1.size() != Opcodes2.size())
9891       return false;
9892     for (int I = 0, E = Opcodes1.size(); I < E; ++I) {
9893       // Undefs are compatible with any other value.
9894       if (isa<UndefValue>(Opcodes1[I]) || isa<UndefValue>(Opcodes2[I]))
9895         continue;
9896       if (auto *I1 = dyn_cast<Instruction>(Opcodes1[I]))
9897         if (auto *I2 = dyn_cast<Instruction>(Opcodes2[I])) {
9898           if (I1->getParent() != I2->getParent())
9899             return false;
9900           InstructionsState S = getSameOpcode({I1, I2});
9901           if (S.getOpcode())
9902             continue;
9903           return false;
9904         }
9905       if (isa<Constant>(Opcodes1[I]) && isa<Constant>(Opcodes2[I]))
9906         continue;
9907       if (Opcodes1[I]->getValueID() != Opcodes2[I]->getValueID())
9908         return false;
9909     }
9910     return true;
9911   };
9912   auto Limit = [&R](Value *V) {
9913     unsigned EltSize = R.getVectorElementSize(V);
9914     return std::max(2U, R.getMaxVecRegSize() / EltSize);
9915   };
9916 
9917   bool HaveVectorizedPhiNodes = false;
9918   do {
9919     // Collect the incoming values from the PHIs.
9920     Incoming.clear();
9921     for (Instruction &I : *BB) {
9922       PHINode *P = dyn_cast<PHINode>(&I);
9923       if (!P)
9924         break;
9925 
9926       // No need to analyze deleted, vectorized and non-vectorizable
9927       // instructions.
9928       if (!VisitedInstrs.count(P) && !R.isDeleted(P) &&
9929           isValidElementType(P->getType()))
9930         Incoming.push_back(P);
9931     }
9932 
9933     // Find the corresponding non-phi nodes for better matching when trying to
9934     // build the tree.
9935     for (Value *V : Incoming) {
9936       SmallVectorImpl<Value *> &Opcodes =
9937           PHIToOpcodes.try_emplace(V).first->getSecond();
9938       if (!Opcodes.empty())
9939         continue;
9940       SmallVector<Value *, 4> Nodes(1, V);
9941       SmallPtrSet<Value *, 4> Visited;
9942       while (!Nodes.empty()) {
9943         auto *PHI = cast<PHINode>(Nodes.pop_back_val());
9944         if (!Visited.insert(PHI).second)
9945           continue;
9946         for (Value *V : PHI->incoming_values()) {
9947           if (auto *PHI1 = dyn_cast<PHINode>((V))) {
9948             Nodes.push_back(PHI1);
9949             continue;
9950           }
9951           Opcodes.emplace_back(V);
9952         }
9953       }
9954     }
9955 
9956     HaveVectorizedPhiNodes = tryToVectorizeSequence<Value>(
9957         Incoming, Limit, PHICompare, AreCompatiblePHIs,
9958         [this, &R](ArrayRef<Value *> Candidates, bool LimitForRegisterSize) {
9959           return tryToVectorizeList(Candidates, R, LimitForRegisterSize);
9960         },
9961         /*LimitForRegisterSize=*/true);
9962     Changed |= HaveVectorizedPhiNodes;
9963     VisitedInstrs.insert(Incoming.begin(), Incoming.end());
9964   } while (HaveVectorizedPhiNodes);
9965 
9966   VisitedInstrs.clear();
9967 
9968   SmallVector<Instruction *, 8> PostProcessInstructions;
9969   SmallDenseSet<Instruction *, 4> KeyNodes;
9970   for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
9971     // Skip instructions with scalable type. The num of elements is unknown at
9972     // compile-time for scalable type.
9973     if (isa<ScalableVectorType>(it->getType()))
9974       continue;
9975 
9976     // Skip instructions marked for the deletion.
9977     if (R.isDeleted(&*it))
9978       continue;
9979     // We may go through BB multiple times so skip the one we have checked.
9980     if (!VisitedInstrs.insert(&*it).second) {
9981       if (it->use_empty() && KeyNodes.contains(&*it) &&
9982           vectorizeSimpleInstructions(PostProcessInstructions, BB, R,
9983                                       it->isTerminator())) {
9984         // We would like to start over since some instructions are deleted
9985         // and the iterator may become invalid value.
9986         Changed = true;
9987         it = BB->begin();
9988         e = BB->end();
9989       }
9990       continue;
9991     }
9992 
9993     if (isa<DbgInfoIntrinsic>(it))
9994       continue;
9995 
9996     // Try to vectorize reductions that use PHINodes.
9997     if (PHINode *P = dyn_cast<PHINode>(it)) {
9998       // Check that the PHI is a reduction PHI.
9999       if (P->getNumIncomingValues() == 2) {
10000         // Try to match and vectorize a horizontal reduction.
10001         if (vectorizeRootInstruction(P, getReductionValue(DT, P, BB, LI), BB, R,
10002                                      TTI)) {
10003           Changed = true;
10004           it = BB->begin();
10005           e = BB->end();
10006           continue;
10007         }
10008       }
10009       // Try to vectorize the incoming values of the PHI, to catch reductions
10010       // that feed into PHIs.
10011       for (unsigned I = 0, E = P->getNumIncomingValues(); I != E; I++) {
10012         // Skip if the incoming block is the current BB for now. Also, bypass
10013         // unreachable IR for efficiency and to avoid crashing.
10014         // TODO: Collect the skipped incoming values and try to vectorize them
10015         // after processing BB.
10016         if (BB == P->getIncomingBlock(I) ||
10017             !DT->isReachableFromEntry(P->getIncomingBlock(I)))
10018           continue;
10019 
10020         Changed |= vectorizeRootInstruction(nullptr, P->getIncomingValue(I),
10021                                             P->getIncomingBlock(I), R, TTI);
10022       }
10023       continue;
10024     }
10025 
10026     // Ran into an instruction without users, like terminator, or function call
10027     // with ignored return value, store. Ignore unused instructions (basing on
10028     // instruction type, except for CallInst and InvokeInst).
10029     if (it->use_empty() && (it->getType()->isVoidTy() || isa<CallInst>(it) ||
10030                             isa<InvokeInst>(it))) {
10031       KeyNodes.insert(&*it);
10032       bool OpsChanged = false;
10033       if (ShouldStartVectorizeHorAtStore || !isa<StoreInst>(it)) {
10034         for (auto *V : it->operand_values()) {
10035           // Try to match and vectorize a horizontal reduction.
10036           OpsChanged |= vectorizeRootInstruction(nullptr, V, BB, R, TTI);
10037         }
10038       }
10039       // Start vectorization of post-process list of instructions from the
10040       // top-tree instructions to try to vectorize as many instructions as
10041       // possible.
10042       OpsChanged |= vectorizeSimpleInstructions(PostProcessInstructions, BB, R,
10043                                                 it->isTerminator());
10044       if (OpsChanged) {
10045         // We would like to start over since some instructions are deleted
10046         // and the iterator may become invalid value.
10047         Changed = true;
10048         it = BB->begin();
10049         e = BB->end();
10050         continue;
10051       }
10052     }
10053 
10054     if (isa<InsertElementInst>(it) || isa<CmpInst>(it) ||
10055         isa<InsertValueInst>(it))
10056       PostProcessInstructions.push_back(&*it);
10057   }
10058 
10059   return Changed;
10060 }
10061 
10062 bool SLPVectorizerPass::vectorizeGEPIndices(BasicBlock *BB, BoUpSLP &R) {
10063   auto Changed = false;
10064   for (auto &Entry : GEPs) {
10065     // If the getelementptr list has fewer than two elements, there's nothing
10066     // to do.
10067     if (Entry.second.size() < 2)
10068       continue;
10069 
10070     LLVM_DEBUG(dbgs() << "SLP: Analyzing a getelementptr list of length "
10071                       << Entry.second.size() << ".\n");
10072 
10073     // Process the GEP list in chunks suitable for the target's supported
10074     // vector size. If a vector register can't hold 1 element, we are done. We
10075     // are trying to vectorize the index computations, so the maximum number of
10076     // elements is based on the size of the index expression, rather than the
10077     // size of the GEP itself (the target's pointer size).
10078     unsigned MaxVecRegSize = R.getMaxVecRegSize();
10079     unsigned EltSize = R.getVectorElementSize(*Entry.second[0]->idx_begin());
10080     if (MaxVecRegSize < EltSize)
10081       continue;
10082 
10083     unsigned MaxElts = MaxVecRegSize / EltSize;
10084     for (unsigned BI = 0, BE = Entry.second.size(); BI < BE; BI += MaxElts) {
10085       auto Len = std::min<unsigned>(BE - BI, MaxElts);
10086       ArrayRef<GetElementPtrInst *> GEPList(&Entry.second[BI], Len);
10087 
10088       // Initialize a set a candidate getelementptrs. Note that we use a
10089       // SetVector here to preserve program order. If the index computations
10090       // are vectorizable and begin with loads, we want to minimize the chance
10091       // of having to reorder them later.
10092       SetVector<Value *> Candidates(GEPList.begin(), GEPList.end());
10093 
10094       // Some of the candidates may have already been vectorized after we
10095       // initially collected them. If so, they are marked as deleted, so remove
10096       // them from the set of candidates.
10097       Candidates.remove_if(
10098           [&R](Value *I) { return R.isDeleted(cast<Instruction>(I)); });
10099 
10100       // Remove from the set of candidates all pairs of getelementptrs with
10101       // constant differences. Such getelementptrs are likely not good
10102       // candidates for vectorization in a bottom-up phase since one can be
10103       // computed from the other. We also ensure all candidate getelementptr
10104       // indices are unique.
10105       for (int I = 0, E = GEPList.size(); I < E && Candidates.size() > 1; ++I) {
10106         auto *GEPI = GEPList[I];
10107         if (!Candidates.count(GEPI))
10108           continue;
10109         auto *SCEVI = SE->getSCEV(GEPList[I]);
10110         for (int J = I + 1; J < E && Candidates.size() > 1; ++J) {
10111           auto *GEPJ = GEPList[J];
10112           auto *SCEVJ = SE->getSCEV(GEPList[J]);
10113           if (isa<SCEVConstant>(SE->getMinusSCEV(SCEVI, SCEVJ))) {
10114             Candidates.remove(GEPI);
10115             Candidates.remove(GEPJ);
10116           } else if (GEPI->idx_begin()->get() == GEPJ->idx_begin()->get()) {
10117             Candidates.remove(GEPJ);
10118           }
10119         }
10120       }
10121 
10122       // We break out of the above computation as soon as we know there are
10123       // fewer than two candidates remaining.
10124       if (Candidates.size() < 2)
10125         continue;
10126 
10127       // Add the single, non-constant index of each candidate to the bundle. We
10128       // ensured the indices met these constraints when we originally collected
10129       // the getelementptrs.
10130       SmallVector<Value *, 16> Bundle(Candidates.size());
10131       auto BundleIndex = 0u;
10132       for (auto *V : Candidates) {
10133         auto *GEP = cast<GetElementPtrInst>(V);
10134         auto *GEPIdx = GEP->idx_begin()->get();
10135         assert(GEP->getNumIndices() == 1 || !isa<Constant>(GEPIdx));
10136         Bundle[BundleIndex++] = GEPIdx;
10137       }
10138 
10139       // Try and vectorize the indices. We are currently only interested in
10140       // gather-like cases of the form:
10141       //
10142       // ... = g[a[0] - b[0]] + g[a[1] - b[1]] + ...
10143       //
10144       // where the loads of "a", the loads of "b", and the subtractions can be
10145       // performed in parallel. It's likely that detecting this pattern in a
10146       // bottom-up phase will be simpler and less costly than building a
10147       // full-blown top-down phase beginning at the consecutive loads.
10148       Changed |= tryToVectorizeList(Bundle, R);
10149     }
10150   }
10151   return Changed;
10152 }
10153 
10154 bool SLPVectorizerPass::vectorizeStoreChains(BoUpSLP &R) {
10155   bool Changed = false;
10156   // Sort by type, base pointers and values operand. Value operands must be
10157   // compatible (have the same opcode, same parent), otherwise it is
10158   // definitely not profitable to try to vectorize them.
10159   auto &&StoreSorter = [this](StoreInst *V, StoreInst *V2) {
10160     if (V->getPointerOperandType()->getTypeID() <
10161         V2->getPointerOperandType()->getTypeID())
10162       return true;
10163     if (V->getPointerOperandType()->getTypeID() >
10164         V2->getPointerOperandType()->getTypeID())
10165       return false;
10166     // UndefValues are compatible with all other values.
10167     if (isa<UndefValue>(V->getValueOperand()) ||
10168         isa<UndefValue>(V2->getValueOperand()))
10169       return false;
10170     if (auto *I1 = dyn_cast<Instruction>(V->getValueOperand()))
10171       if (auto *I2 = dyn_cast<Instruction>(V2->getValueOperand())) {
10172         DomTreeNodeBase<llvm::BasicBlock> *NodeI1 =
10173             DT->getNode(I1->getParent());
10174         DomTreeNodeBase<llvm::BasicBlock> *NodeI2 =
10175             DT->getNode(I2->getParent());
10176         assert(NodeI1 && "Should only process reachable instructions");
10177         assert(NodeI1 && "Should only process reachable instructions");
10178         assert((NodeI1 == NodeI2) ==
10179                    (NodeI1->getDFSNumIn() == NodeI2->getDFSNumIn()) &&
10180                "Different nodes should have different DFS numbers");
10181         if (NodeI1 != NodeI2)
10182           return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
10183         InstructionsState S = getSameOpcode({I1, I2});
10184         if (S.getOpcode())
10185           return false;
10186         return I1->getOpcode() < I2->getOpcode();
10187       }
10188     if (isa<Constant>(V->getValueOperand()) &&
10189         isa<Constant>(V2->getValueOperand()))
10190       return false;
10191     return V->getValueOperand()->getValueID() <
10192            V2->getValueOperand()->getValueID();
10193   };
10194 
10195   auto &&AreCompatibleStores = [](StoreInst *V1, StoreInst *V2) {
10196     if (V1 == V2)
10197       return true;
10198     if (V1->getPointerOperandType() != V2->getPointerOperandType())
10199       return false;
10200     // Undefs are compatible with any other value.
10201     if (isa<UndefValue>(V1->getValueOperand()) ||
10202         isa<UndefValue>(V2->getValueOperand()))
10203       return true;
10204     if (auto *I1 = dyn_cast<Instruction>(V1->getValueOperand()))
10205       if (auto *I2 = dyn_cast<Instruction>(V2->getValueOperand())) {
10206         if (I1->getParent() != I2->getParent())
10207           return false;
10208         InstructionsState S = getSameOpcode({I1, I2});
10209         return S.getOpcode() > 0;
10210       }
10211     if (isa<Constant>(V1->getValueOperand()) &&
10212         isa<Constant>(V2->getValueOperand()))
10213       return true;
10214     return V1->getValueOperand()->getValueID() ==
10215            V2->getValueOperand()->getValueID();
10216   };
10217   auto Limit = [&R, this](StoreInst *SI) {
10218     unsigned EltSize = DL->getTypeSizeInBits(SI->getValueOperand()->getType());
10219     return R.getMinVF(EltSize);
10220   };
10221 
10222   // Attempt to sort and vectorize each of the store-groups.
10223   for (auto &Pair : Stores) {
10224     if (Pair.second.size() < 2)
10225       continue;
10226 
10227     LLVM_DEBUG(dbgs() << "SLP: Analyzing a store chain of length "
10228                       << Pair.second.size() << ".\n");
10229 
10230     if (!isValidElementType(Pair.second.front()->getValueOperand()->getType()))
10231       continue;
10232 
10233     Changed |= tryToVectorizeSequence<StoreInst>(
10234         Pair.second, Limit, StoreSorter, AreCompatibleStores,
10235         [this, &R](ArrayRef<StoreInst *> Candidates, bool) {
10236           return vectorizeStores(Candidates, R);
10237         },
10238         /*LimitForRegisterSize=*/false);
10239   }
10240   return Changed;
10241 }
10242 
10243 char SLPVectorizer::ID = 0;
10244 
10245 static const char lv_name[] = "SLP Vectorizer";
10246 
10247 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
10248 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
10249 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
10250 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
10251 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
10252 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
10253 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
10254 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
10255 INITIALIZE_PASS_DEPENDENCY(InjectTLIMappingsLegacy)
10256 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
10257 
10258 Pass *llvm::createSLPVectorizerPass() { return new SLPVectorizer(); }
10259