1 //===------- VectorCombine.cpp - Optimize partial vector operations -------===//
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 optimizes scalar/vector interactions using target cost models. The
10 // transforms implemented here may not fit in traditional loop-based or SLP
11 // vectorization passes.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Vectorize/VectorCombine.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/Analysis/BasicAliasAnalysis.h"
18 #include "llvm/Analysis/GlobalsModRef.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/Analysis/VectorUtils.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/PatternMatch.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 #include "llvm/Transforms/Vectorize.h"
31 
32 using namespace llvm;
33 using namespace llvm::PatternMatch;
34 
35 #define DEBUG_TYPE "vector-combine"
36 STATISTIC(NumVecCmp, "Number of vector compares formed");
37 STATISTIC(NumVecBO, "Number of vector binops formed");
38 STATISTIC(NumShufOfBitcast, "Number of shuffles moved after bitcast");
39 STATISTIC(NumScalarBO, "Number of scalar binops formed");
40 STATISTIC(NumScalarCmp, "Number of scalar compares formed");
41 
42 static cl::opt<bool> DisableVectorCombine(
43     "disable-vector-combine", cl::init(false), cl::Hidden,
44     cl::desc("Disable all vector combine transforms"));
45 
46 static cl::opt<bool> DisableBinopExtractShuffle(
47     "disable-binop-extract-shuffle", cl::init(false), cl::Hidden,
48     cl::desc("Disable binop extract to shuffle transforms"));
49 
50 
51 /// Compare the relative costs of 2 extracts followed by scalar operation vs.
52 /// vector operation(s) followed by extract. Return true if the existing
53 /// instructions are cheaper than a vector alternative. Otherwise, return false
54 /// and if one of the extracts should be transformed to a shufflevector, set
55 /// \p ConvertToShuffle to that extract instruction.
56 static bool isExtractExtractCheap(Instruction *Ext0, Instruction *Ext1,
57                                   unsigned Opcode,
58                                   const TargetTransformInfo &TTI,
59                                   Instruction *&ConvertToShuffle,
60                                   unsigned PreferredExtractIndex) {
61   assert(isa<ConstantInt>(Ext0->getOperand(1)) &&
62          isa<ConstantInt>(Ext1->getOperand(1)) &&
63          "Expected constant extract indexes");
64   Type *ScalarTy = Ext0->getType();
65   auto *VecTy = cast<VectorType>(Ext0->getOperand(0)->getType());
66   int ScalarOpCost, VectorOpCost;
67 
68   // Get cost estimates for scalar and vector versions of the operation.
69   bool IsBinOp = Instruction::isBinaryOp(Opcode);
70   if (IsBinOp) {
71     ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy);
72     VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy);
73   } else {
74     assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
75            "Expected a compare");
76     ScalarOpCost = TTI.getCmpSelInstrCost(Opcode, ScalarTy,
77                                           CmpInst::makeCmpResultType(ScalarTy));
78     VectorOpCost = TTI.getCmpSelInstrCost(Opcode, VecTy,
79                                           CmpInst::makeCmpResultType(VecTy));
80   }
81 
82   // Get cost estimates for the extract elements. These costs will factor into
83   // both sequences.
84   unsigned Ext0Index = cast<ConstantInt>(Ext0->getOperand(1))->getZExtValue();
85   unsigned Ext1Index = cast<ConstantInt>(Ext1->getOperand(1))->getZExtValue();
86 
87   int Extract0Cost = TTI.getVectorInstrCost(Instruction::ExtractElement,
88                                             VecTy, Ext0Index);
89   int Extract1Cost = TTI.getVectorInstrCost(Instruction::ExtractElement,
90                                             VecTy, Ext1Index);
91 
92   // A more expensive extract will always be replaced by a splat shuffle.
93   // For example, if Ext0 is more expensive:
94   // opcode (extelt V0, Ext0), (ext V1, Ext1) -->
95   // extelt (opcode (splat V0, Ext0), V1), Ext1
96   // TODO: Evaluate whether that always results in lowest cost. Alternatively,
97   //       check the cost of creating a broadcast shuffle and shuffling both
98   //       operands to element 0.
99   int CheapExtractCost = std::min(Extract0Cost, Extract1Cost);
100 
101   // Extra uses of the extracts mean that we include those costs in the
102   // vector total because those instructions will not be eliminated.
103   int OldCost, NewCost;
104   if (Ext0->getOperand(0) == Ext1->getOperand(0) && Ext0Index == Ext1Index) {
105     // Handle a special case. If the 2 extracts are identical, adjust the
106     // formulas to account for that. The extra use charge allows for either the
107     // CSE'd pattern or an unoptimized form with identical values:
108     // opcode (extelt V, C), (extelt V, C) --> extelt (opcode V, V), C
109     bool HasUseTax = Ext0 == Ext1 ? !Ext0->hasNUses(2)
110                                   : !Ext0->hasOneUse() || !Ext1->hasOneUse();
111     OldCost = CheapExtractCost + ScalarOpCost;
112     NewCost = VectorOpCost + CheapExtractCost + HasUseTax * CheapExtractCost;
113   } else {
114     // Handle the general case. Each extract is actually a different value:
115     // opcode (extelt V0, C0), (extelt V1, C1) --> extelt (opcode V0, V1), C
116     OldCost = Extract0Cost + Extract1Cost + ScalarOpCost;
117     NewCost = VectorOpCost + CheapExtractCost +
118               !Ext0->hasOneUse() * Extract0Cost +
119               !Ext1->hasOneUse() * Extract1Cost;
120   }
121 
122   if (Ext0Index == Ext1Index) {
123     // If the extract indexes are identical, no shuffle is needed.
124     ConvertToShuffle = nullptr;
125   } else {
126     if (IsBinOp && DisableBinopExtractShuffle)
127       return true;
128 
129     // If we are extracting from 2 different indexes, then one operand must be
130     // shuffled before performing the vector operation. The shuffle mask is
131     // undefined except for 1 lane that is being translated to the remaining
132     // extraction lane. Therefore, it is a splat shuffle. Ex:
133     // ShufMask = { undef, undef, 0, undef }
134     // TODO: The cost model has an option for a "broadcast" shuffle
135     //       (splat-from-element-0), but no option for a more general splat.
136     NewCost +=
137         TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, VecTy);
138 
139     // The more expensive extract will be replaced by a shuffle. If the costs
140     // are equal and there is a preferred extract index, shuffle the opposite
141     // operand. Otherwise, replace the extract with the higher index.
142     if (Extract0Cost > Extract1Cost)
143       ConvertToShuffle = Ext0;
144     else if (Extract1Cost > Extract0Cost)
145       ConvertToShuffle = Ext1;
146     else if (PreferredExtractIndex == Ext0Index)
147       ConvertToShuffle = Ext1;
148     else if (PreferredExtractIndex == Ext1Index)
149       ConvertToShuffle = Ext0;
150     else
151       ConvertToShuffle = Ext0Index > Ext1Index ? Ext0 : Ext1;
152   }
153 
154   // Aggressively form a vector op if the cost is equal because the transform
155   // may enable further optimization.
156   // Codegen can reverse this transform (scalarize) if it was not profitable.
157   return OldCost < NewCost;
158 }
159 
160 /// Try to reduce extract element costs by converting scalar compares to vector
161 /// compares followed by extract.
162 /// cmp (ext0 V0, C), (ext1 V1, C)
163 static void foldExtExtCmp(Instruction *Ext0, Instruction *Ext1,
164                           Instruction &I) {
165   assert(isa<CmpInst>(&I) && "Expected a compare");
166 
167   // cmp Pred (extelt V0, C), (extelt V1, C) --> extelt (cmp Pred V0, V1), C
168   ++NumVecCmp;
169   IRBuilder<> Builder(&I);
170   CmpInst::Predicate Pred = cast<CmpInst>(&I)->getPredicate();
171   Value *V0 = Ext0->getOperand(0), *V1 = Ext1->getOperand(0);
172   Value *VecCmp = Builder.CreateCmp(Pred, V0, V1);
173   Value *Extract = Builder.CreateExtractElement(VecCmp, Ext0->getOperand(1));
174   I.replaceAllUsesWith(Extract);
175 }
176 
177 /// Try to reduce extract element costs by converting scalar binops to vector
178 /// binops followed by extract.
179 /// bo (ext0 V0, C), (ext1 V1, C)
180 static void foldExtExtBinop(Instruction *Ext0, Instruction *Ext1,
181                             Instruction &I) {
182   assert(isa<BinaryOperator>(&I) && "Expected a binary operator");
183 
184   // bo (extelt V0, C), (extelt V1, C) --> extelt (bo V0, V1), C
185   ++NumVecBO;
186   IRBuilder<> Builder(&I);
187   Value *V0 = Ext0->getOperand(0), *V1 = Ext1->getOperand(0);
188   Value *VecBO =
189       Builder.CreateBinOp(cast<BinaryOperator>(&I)->getOpcode(), V0, V1);
190 
191   // All IR flags are safe to back-propagate because any potential poison
192   // created in unused vector elements is discarded by the extract.
193   if (auto *VecBOInst = dyn_cast<Instruction>(VecBO))
194     VecBOInst->copyIRFlags(&I);
195 
196   Value *Extract = Builder.CreateExtractElement(VecBO, Ext0->getOperand(1));
197   I.replaceAllUsesWith(Extract);
198 }
199 
200 /// Match an instruction with extracted vector operands.
201 static bool foldExtractExtract(Instruction &I, const TargetTransformInfo &TTI) {
202   // It is not safe to transform things like div, urem, etc. because we may
203   // create undefined behavior when executing those on unknown vector elements.
204   if (!isSafeToSpeculativelyExecute(&I))
205     return false;
206 
207   Instruction *Ext0, *Ext1;
208   CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
209   if (!match(&I, m_Cmp(Pred, m_Instruction(Ext0), m_Instruction(Ext1))) &&
210       !match(&I, m_BinOp(m_Instruction(Ext0), m_Instruction(Ext1))))
211     return false;
212 
213   Value *V0, *V1;
214   uint64_t C0, C1;
215   if (!match(Ext0, m_ExtractElt(m_Value(V0), m_ConstantInt(C0))) ||
216       !match(Ext1, m_ExtractElt(m_Value(V1), m_ConstantInt(C1))) ||
217       V0->getType() != V1->getType())
218     return false;
219 
220   // If the scalar value 'I' is going to be re-inserted into a vector, then try
221   // to create an extract to that same element. The extract/insert can be
222   // reduced to a "select shuffle".
223   // TODO: If we add a larger pattern match that starts from an insert, this
224   //       probably becomes unnecessary.
225   uint64_t InsertIndex = std::numeric_limits<uint64_t>::max();
226   if (I.hasOneUse())
227     match(I.user_back(),
228           m_InsertElt(m_Value(), m_Value(), m_ConstantInt(InsertIndex)));
229 
230   Instruction *ConvertToShuffle;
231   if (isExtractExtractCheap(Ext0, Ext1, I.getOpcode(), TTI, ConvertToShuffle,
232                             InsertIndex))
233     return false;
234 
235   if (ConvertToShuffle) {
236     // If the extract can be constant-folded, this code is unsimplified. Defer
237     // to other passes to handle that.
238     if (isa<Constant>(ConvertToShuffle->getOperand(0)))
239       return false;
240 
241     // The shuffle mask is undefined except for 1 lane that is being translated
242     // to the cheap extraction lane. Example:
243     // ShufMask = { 2, undef, undef, undef }
244     uint64_t SplatIndex = ConvertToShuffle == Ext0 ? C0 : C1;
245     uint64_t CheapExtIndex = ConvertToShuffle == Ext0 ? C1 : C0;
246     auto *VecTy = cast<VectorType>(V0->getType());
247     SmallVector<int, 32> ShufMask(VecTy->getNumElements(), -1);
248     ShufMask[CheapExtIndex] = SplatIndex;
249     IRBuilder<> Builder(ConvertToShuffle);
250 
251     // extelt X, C --> extelt (splat X), C'
252     Value *Shuf = Builder.CreateShuffleVector(ConvertToShuffle->getOperand(0),
253                                               UndefValue::get(VecTy), ShufMask);
254     Value *NewExt = Builder.CreateExtractElement(Shuf, CheapExtIndex);
255     if (ConvertToShuffle == Ext0)
256       Ext0 = cast<Instruction>(NewExt);
257     else
258       Ext1 = cast<Instruction>(NewExt);
259   }
260 
261   if (Pred != CmpInst::BAD_ICMP_PREDICATE)
262     foldExtExtCmp(Ext0, Ext1, I);
263   else
264     foldExtExtBinop(Ext0, Ext1, I);
265 
266   return true;
267 }
268 
269 /// If this is a bitcast of a shuffle, try to bitcast the source vector to the
270 /// destination type followed by shuffle. This can enable further transforms by
271 /// moving bitcasts or shuffles together.
272 static bool foldBitcastShuf(Instruction &I, const TargetTransformInfo &TTI) {
273   Value *V;
274   ArrayRef<int> Mask;
275   if (!match(&I, m_BitCast(
276                      m_OneUse(m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask))))))
277     return false;
278 
279   // Disallow non-vector casts and length-changing shuffles.
280   // TODO: We could allow any shuffle.
281   auto *DestTy = dyn_cast<VectorType>(I.getType());
282   auto *SrcTy = cast<VectorType>(V->getType());
283   if (!DestTy || I.getOperand(0)->getType() != SrcTy)
284     return false;
285 
286   // The new shuffle must not cost more than the old shuffle. The bitcast is
287   // moved ahead of the shuffle, so assume that it has the same cost as before.
288   if (TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, DestTy) >
289       TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, SrcTy))
290     return false;
291 
292   unsigned DestNumElts = DestTy->getNumElements();
293   unsigned SrcNumElts = SrcTy->getNumElements();
294   SmallVector<int, 16> NewMask;
295   if (SrcNumElts <= DestNumElts) {
296     // The bitcast is from wide to narrow/equal elements. The shuffle mask can
297     // always be expanded to the equivalent form choosing narrower elements.
298     assert(DestNumElts % SrcNumElts == 0 && "Unexpected shuffle mask");
299     unsigned ScaleFactor = DestNumElts / SrcNumElts;
300     narrowShuffleMaskElts(ScaleFactor, Mask, NewMask);
301   } else {
302     // The bitcast is from narrow elements to wide elements. The shuffle mask
303     // must choose consecutive elements to allow casting first.
304     assert(SrcNumElts % DestNumElts == 0 && "Unexpected shuffle mask");
305     unsigned ScaleFactor = SrcNumElts / DestNumElts;
306     if (!widenShuffleMaskElts(ScaleFactor, Mask, NewMask))
307       return false;
308   }
309   // bitcast (shuf V, MaskC) --> shuf (bitcast V), MaskC'
310   ++NumShufOfBitcast;
311   IRBuilder<> Builder(&I);
312   Value *CastV = Builder.CreateBitCast(V, DestTy);
313   Value *Shuf =
314       Builder.CreateShuffleVector(CastV, UndefValue::get(DestTy), NewMask);
315   I.replaceAllUsesWith(Shuf);
316   return true;
317 }
318 
319 /// Match a vector binop or compare instruction with at least one inserted
320 /// scalar operand and convert to scalar binop/cmp followed by insertelement.
321 static bool scalarizeBinopOrCmp(Instruction &I,
322                                 const TargetTransformInfo &TTI) {
323   CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
324   Value *Ins0, *Ins1;
325   if (!match(&I, m_BinOp(m_Value(Ins0), m_Value(Ins1))) &&
326       !match(&I, m_Cmp(Pred, m_Value(Ins0), m_Value(Ins1))))
327     return false;
328 
329   // Do not convert the vector condition of a vector select into a scalar
330   // condition. That may cause problems for codegen because of differences in
331   // boolean formats and register-file transfers.
332   // TODO: Can we account for that in the cost model?
333   bool IsCmp = Pred != CmpInst::Predicate::BAD_ICMP_PREDICATE;
334   if (IsCmp)
335     for (User *U : I.users())
336       if (match(U, m_Select(m_Specific(&I), m_Value(), m_Value())))
337         return false;
338 
339   // Match against one or both scalar values being inserted into constant
340   // vectors:
341   // vec_op VecC0, (inselt VecC1, V1, Index)
342   // vec_op (inselt VecC0, V0, Index), VecC1
343   // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index)
344   // TODO: Deal with mismatched index constants and variable indexes?
345   Constant *VecC0 = nullptr, *VecC1 = nullptr;
346   Value *V0 = nullptr, *V1 = nullptr;
347   uint64_t Index0 = 0, Index1 = 0;
348   if (!match(Ins0, m_InsertElt(m_Constant(VecC0), m_Value(V0),
349                                m_ConstantInt(Index0))) &&
350       !match(Ins0, m_Constant(VecC0)))
351     return false;
352   if (!match(Ins1, m_InsertElt(m_Constant(VecC1), m_Value(V1),
353                                m_ConstantInt(Index1))) &&
354       !match(Ins1, m_Constant(VecC1)))
355     return false;
356 
357   bool IsConst0 = !V0;
358   bool IsConst1 = !V1;
359   if (IsConst0 && IsConst1)
360     return false;
361   if (!IsConst0 && !IsConst1 && Index0 != Index1)
362     return false;
363 
364   // Bail for single insertion if it is a load.
365   // TODO: Handle this once getVectorInstrCost can cost for load/stores.
366   auto *I0 = dyn_cast_or_null<Instruction>(V0);
367   auto *I1 = dyn_cast_or_null<Instruction>(V1);
368   if ((IsConst0 && I1 && I1->mayReadFromMemory()) ||
369       (IsConst1 && I0 && I0->mayReadFromMemory()))
370     return false;
371 
372   uint64_t Index = IsConst0 ? Index1 : Index0;
373   Type *ScalarTy = IsConst0 ? V1->getType() : V0->getType();
374   Type *VecTy = I.getType();
375   assert(VecTy->isVectorTy() &&
376          (IsConst0 || IsConst1 || V0->getType() == V1->getType()) &&
377          (ScalarTy->isIntegerTy() || ScalarTy->isFloatingPointTy()) &&
378          "Unexpected types for insert into binop");
379 
380   unsigned Opcode = I.getOpcode();
381   int ScalarOpCost, VectorOpCost;
382   if (IsCmp) {
383     ScalarOpCost = TTI.getCmpSelInstrCost(Opcode, ScalarTy);
384     VectorOpCost = TTI.getCmpSelInstrCost(Opcode, VecTy);
385   } else {
386     ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy);
387     VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy);
388   }
389 
390   // Get cost estimate for the insert element. This cost will factor into
391   // both sequences.
392   int InsertCost =
393       TTI.getVectorInstrCost(Instruction::InsertElement, VecTy, Index);
394   int OldCost = (IsConst0 ? 0 : InsertCost) + (IsConst1 ? 0 : InsertCost) +
395                 VectorOpCost;
396   int NewCost = ScalarOpCost + InsertCost +
397                 (IsConst0 ? 0 : !Ins0->hasOneUse() * InsertCost) +
398                 (IsConst1 ? 0 : !Ins1->hasOneUse() * InsertCost);
399 
400   // We want to scalarize unless the vector variant actually has lower cost.
401   if (OldCost < NewCost)
402     return false;
403 
404   // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index) -->
405   // inselt NewVecC, (scalar_op V0, V1), Index
406   if (IsCmp)
407     ++NumScalarCmp;
408   else
409     ++NumScalarBO;
410 
411   // For constant cases, extract the scalar element, this should constant fold.
412   IRBuilder<> Builder(&I);
413   if (IsConst0)
414     V0 = ConstantExpr::getExtractElement(VecC0, Builder.getInt64(Index));
415   if (IsConst1)
416     V1 = ConstantExpr::getExtractElement(VecC1, Builder.getInt64(Index));
417 
418   Value *Scalar =
419       IsCmp ? Builder.CreateCmp(Pred, V0, V1)
420             : Builder.CreateBinOp((Instruction::BinaryOps)Opcode, V0, V1);
421 
422   Scalar->setName(I.getName() + ".scalar");
423 
424   // All IR flags are safe to back-propagate. There is no potential for extra
425   // poison to be created by the scalar instruction.
426   if (auto *ScalarInst = dyn_cast<Instruction>(Scalar))
427     ScalarInst->copyIRFlags(&I);
428 
429   // Fold the vector constants in the original vectors into a new base vector.
430   Constant *NewVecC = IsCmp ? ConstantExpr::getCompare(Pred, VecC0, VecC1)
431                             : ConstantExpr::get(Opcode, VecC0, VecC1);
432   Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, Index);
433   I.replaceAllUsesWith(Insert);
434   Insert->takeName(&I);
435   return true;
436 }
437 
438 /// This is the entry point for all transforms. Pass manager differences are
439 /// handled in the callers of this function.
440 static bool runImpl(Function &F, const TargetTransformInfo &TTI,
441                     const DominatorTree &DT) {
442   if (DisableVectorCombine)
443     return false;
444 
445   bool MadeChange = false;
446   for (BasicBlock &BB : F) {
447     // Ignore unreachable basic blocks.
448     if (!DT.isReachableFromEntry(&BB))
449       continue;
450     // Do not delete instructions under here and invalidate the iterator.
451     // Walk the block forwards to enable simple iterative chains of transforms.
452     // TODO: It could be more efficient to remove dead instructions
453     //       iteratively in this loop rather than waiting until the end.
454     for (Instruction &I : BB) {
455       if (isa<DbgInfoIntrinsic>(I))
456         continue;
457       MadeChange |= foldExtractExtract(I, TTI);
458       MadeChange |= foldBitcastShuf(I, TTI);
459       MadeChange |= scalarizeBinopOrCmp(I, TTI);
460     }
461   }
462 
463   // We're done with transforms, so remove dead instructions.
464   if (MadeChange)
465     for (BasicBlock &BB : F)
466       SimplifyInstructionsInBlock(&BB);
467 
468   return MadeChange;
469 }
470 
471 // Pass manager boilerplate below here.
472 
473 namespace {
474 class VectorCombineLegacyPass : public FunctionPass {
475 public:
476   static char ID;
477   VectorCombineLegacyPass() : FunctionPass(ID) {
478     initializeVectorCombineLegacyPassPass(*PassRegistry::getPassRegistry());
479   }
480 
481   void getAnalysisUsage(AnalysisUsage &AU) const override {
482     AU.addRequired<DominatorTreeWrapperPass>();
483     AU.addRequired<TargetTransformInfoWrapperPass>();
484     AU.setPreservesCFG();
485     AU.addPreserved<DominatorTreeWrapperPass>();
486     AU.addPreserved<GlobalsAAWrapperPass>();
487     AU.addPreserved<AAResultsWrapperPass>();
488     AU.addPreserved<BasicAAWrapperPass>();
489     FunctionPass::getAnalysisUsage(AU);
490   }
491 
492   bool runOnFunction(Function &F) override {
493     if (skipFunction(F))
494       return false;
495     auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
496     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
497     return runImpl(F, TTI, DT);
498   }
499 };
500 } // namespace
501 
502 char VectorCombineLegacyPass::ID = 0;
503 INITIALIZE_PASS_BEGIN(VectorCombineLegacyPass, "vector-combine",
504                       "Optimize scalar/vector ops", false,
505                       false)
506 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
507 INITIALIZE_PASS_END(VectorCombineLegacyPass, "vector-combine",
508                     "Optimize scalar/vector ops", false, false)
509 Pass *llvm::createVectorCombinePass() {
510   return new VectorCombineLegacyPass();
511 }
512 
513 PreservedAnalyses VectorCombinePass::run(Function &F,
514                                          FunctionAnalysisManager &FAM) {
515   TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F);
516   DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
517   if (!runImpl(F, TTI, DT))
518     return PreservedAnalyses::all();
519   PreservedAnalyses PA;
520   PA.preserveSet<CFGAnalyses>();
521   PA.preserve<GlobalsAA>();
522   PA.preserve<AAManager>();
523   PA.preserve<BasicAA>();
524   return PA;
525 }
526