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 =
173       Ext0->getType()->isFloatingPointTy() ? Builder.CreateFCmp(Pred, V0, V1)
174                                            : Builder.CreateICmp(Pred, V0, V1);
175   Value *Extract = Builder.CreateExtractElement(VecCmp, Ext0->getOperand(1));
176   I.replaceAllUsesWith(Extract);
177 }
178 
179 /// Try to reduce extract element costs by converting scalar binops to vector
180 /// binops followed by extract.
181 /// bo (ext0 V0, C), (ext1 V1, C)
182 static void foldExtExtBinop(Instruction *Ext0, Instruction *Ext1,
183                             Instruction &I) {
184   assert(isa<BinaryOperator>(&I) && "Expected a binary operator");
185 
186   // bo (extelt V0, C), (extelt V1, C) --> extelt (bo V0, V1), C
187   ++NumVecBO;
188   IRBuilder<> Builder(&I);
189   Value *V0 = Ext0->getOperand(0), *V1 = Ext1->getOperand(0);
190   Value *VecBO =
191       Builder.CreateBinOp(cast<BinaryOperator>(&I)->getOpcode(), V0, V1);
192 
193   // All IR flags are safe to back-propagate because any potential poison
194   // created in unused vector elements is discarded by the extract.
195   if (auto *VecBOInst = dyn_cast<Instruction>(VecBO))
196     VecBOInst->copyIRFlags(&I);
197 
198   Value *Extract = Builder.CreateExtractElement(VecBO, Ext0->getOperand(1));
199   I.replaceAllUsesWith(Extract);
200 }
201 
202 /// Match an instruction with extracted vector operands.
203 static bool foldExtractExtract(Instruction &I, const TargetTransformInfo &TTI) {
204   // It is not safe to transform things like div, urem, etc. because we may
205   // create undefined behavior when executing those on unknown vector elements.
206   if (!isSafeToSpeculativelyExecute(&I))
207     return false;
208 
209   Instruction *Ext0, *Ext1;
210   CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
211   if (!match(&I, m_Cmp(Pred, m_Instruction(Ext0), m_Instruction(Ext1))) &&
212       !match(&I, m_BinOp(m_Instruction(Ext0), m_Instruction(Ext1))))
213     return false;
214 
215   Value *V0, *V1;
216   uint64_t C0, C1;
217   if (!match(Ext0, m_ExtractElt(m_Value(V0), m_ConstantInt(C0))) ||
218       !match(Ext1, m_ExtractElt(m_Value(V1), m_ConstantInt(C1))) ||
219       V0->getType() != V1->getType())
220     return false;
221 
222   // If the scalar value 'I' is going to be re-inserted into a vector, then try
223   // to create an extract to that same element. The extract/insert can be
224   // reduced to a "select shuffle".
225   // TODO: If we add a larger pattern match that starts from an insert, this
226   //       probably becomes unnecessary.
227   uint64_t InsertIndex = std::numeric_limits<uint64_t>::max();
228   if (I.hasOneUse())
229     match(I.user_back(),
230           m_InsertElt(m_Value(), m_Value(), m_ConstantInt(InsertIndex)));
231 
232   Instruction *ConvertToShuffle;
233   if (isExtractExtractCheap(Ext0, Ext1, I.getOpcode(), TTI, ConvertToShuffle,
234                             InsertIndex))
235     return false;
236 
237   if (ConvertToShuffle) {
238     // The shuffle mask is undefined except for 1 lane that is being translated
239     // to the cheap extraction lane. Example:
240     // ShufMask = { 2, undef, undef, undef }
241     uint64_t SplatIndex = ConvertToShuffle == Ext0 ? C0 : C1;
242     uint64_t CheapExtIndex = ConvertToShuffle == Ext0 ? C1 : C0;
243     auto *VecTy = cast<VectorType>(V0->getType());
244     SmallVector<int, 32> ShufMask(VecTy->getNumElements(), -1);
245     ShufMask[CheapExtIndex] = SplatIndex;
246     IRBuilder<> Builder(ConvertToShuffle);
247 
248     // extelt X, C --> extelt (splat X), C'
249     Value *Shuf = Builder.CreateShuffleVector(ConvertToShuffle->getOperand(0),
250                                               UndefValue::get(VecTy), ShufMask);
251     Value *NewExt = Builder.CreateExtractElement(Shuf, CheapExtIndex);
252     if (ConvertToShuffle == Ext0)
253       Ext0 = cast<Instruction>(NewExt);
254     else
255       Ext1 = cast<Instruction>(NewExt);
256   }
257 
258   if (Pred != CmpInst::BAD_ICMP_PREDICATE)
259     foldExtExtCmp(Ext0, Ext1, I);
260   else
261     foldExtExtBinop(Ext0, Ext1, I);
262 
263   return true;
264 }
265 
266 /// If this is a bitcast of a shuffle, try to bitcast the source vector to the
267 /// destination type followed by shuffle. This can enable further transforms by
268 /// moving bitcasts or shuffles together.
269 static bool foldBitcastShuf(Instruction &I, const TargetTransformInfo &TTI) {
270   Value *V;
271   ArrayRef<int> Mask;
272   if (!match(&I, m_BitCast(
273                      m_OneUse(m_Shuffle(m_Value(V), m_Undef(), m_Mask(Mask))))))
274     return false;
275 
276   // Disallow non-vector casts and length-changing shuffles.
277   // TODO: We could allow any shuffle.
278   auto *DestTy = dyn_cast<VectorType>(I.getType());
279   auto *SrcTy = cast<VectorType>(V->getType());
280   if (!DestTy || I.getOperand(0)->getType() != SrcTy)
281     return false;
282 
283   // The new shuffle must not cost more than the old shuffle. The bitcast is
284   // moved ahead of the shuffle, so assume that it has the same cost as before.
285   if (TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, DestTy) >
286       TTI.getShuffleCost(TargetTransformInfo::SK_PermuteSingleSrc, SrcTy))
287     return false;
288 
289   unsigned DestNumElts = DestTy->getNumElements();
290   unsigned SrcNumElts = SrcTy->getNumElements();
291   SmallVector<int, 16> NewMask;
292   if (SrcNumElts <= DestNumElts) {
293     // The bitcast is from wide to narrow/equal elements. The shuffle mask can
294     // always be expanded to the equivalent form choosing narrower elements.
295     assert(DestNumElts % SrcNumElts == 0 && "Unexpected shuffle mask");
296     unsigned ScaleFactor = DestNumElts / SrcNumElts;
297     narrowShuffleMaskElts(ScaleFactor, Mask, NewMask);
298   } else {
299     // The bitcast is from narrow elements to wide elements. The shuffle mask
300     // must choose consecutive elements to allow casting first.
301     assert(SrcNumElts % DestNumElts == 0 && "Unexpected shuffle mask");
302     unsigned ScaleFactor = SrcNumElts / DestNumElts;
303     if (!widenShuffleMaskElts(ScaleFactor, Mask, NewMask))
304       return false;
305   }
306   // bitcast (shuf V, MaskC) --> shuf (bitcast V), MaskC'
307   ++NumShufOfBitcast;
308   IRBuilder<> Builder(&I);
309   Value *CastV = Builder.CreateBitCast(V, DestTy);
310   Value *Shuf =
311       Builder.CreateShuffleVector(CastV, UndefValue::get(DestTy), NewMask);
312   I.replaceAllUsesWith(Shuf);
313   return true;
314 }
315 
316 /// Match a vector binop or compare instruction with at least one inserted
317 /// scalar operand and convert to scalar binop/cmp followed by insertelement.
318 static bool scalarizeBinopOrCmp(Instruction &I,
319                                 const TargetTransformInfo &TTI) {
320   CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
321   Value *Ins0, *Ins1;
322   if (!match(&I, m_BinOp(m_Value(Ins0), m_Value(Ins1))) &&
323       !match(&I, m_Cmp(Pred, m_Value(Ins0), m_Value(Ins1))))
324     return false;
325 
326   // Do not convert the vector condition of a vector select into a scalar
327   // condition. That may cause problems for codegen because of differences in
328   // boolean formats and register-file transfers.
329   // TODO: Can we account for that in the cost model?
330   bool IsCmp = Pred != CmpInst::Predicate::BAD_ICMP_PREDICATE;
331   if (IsCmp)
332     for (User *U : I.users())
333       if (match(U, m_Select(m_Specific(&I), m_Value(), m_Value())))
334         return false;
335 
336   // Match against one or both scalar values being inserted into constant
337   // vectors:
338   // vec_op VecC0, (inselt VecC1, V1, Index)
339   // vec_op (inselt VecC0, V0, Index), VecC1
340   // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index)
341   // TODO: Deal with mismatched index constants and variable indexes?
342   Constant *VecC0 = nullptr, *VecC1 = nullptr;
343   Value *V0 = nullptr, *V1 = nullptr;
344   uint64_t Index0 = 0, Index1 = 0;
345   if (!match(Ins0, m_InsertElt(m_Constant(VecC0), m_Value(V0),
346                                m_ConstantInt(Index0))) &&
347       !match(Ins0, m_Constant(VecC0)))
348     return false;
349   if (!match(Ins1, m_InsertElt(m_Constant(VecC1), m_Value(V1),
350                                m_ConstantInt(Index1))) &&
351       !match(Ins1, m_Constant(VecC1)))
352     return false;
353 
354   bool IsConst0 = !V0;
355   bool IsConst1 = !V1;
356   if (IsConst0 && IsConst1)
357     return false;
358   if (!IsConst0 && !IsConst1 && Index0 != Index1)
359     return false;
360 
361   // Bail for single insertion if it is a load.
362   // TODO: Handle this once getVectorInstrCost can cost for load/stores.
363   auto *I0 = dyn_cast_or_null<Instruction>(V0);
364   auto *I1 = dyn_cast_or_null<Instruction>(V1);
365   if ((IsConst0 && I1 && I1->mayReadFromMemory()) ||
366       (IsConst1 && I0 && I0->mayReadFromMemory()))
367     return false;
368 
369   uint64_t Index = IsConst0 ? Index1 : Index0;
370   Type *ScalarTy = IsConst0 ? V1->getType() : V0->getType();
371   Type *VecTy = I.getType();
372   assert(VecTy->isVectorTy() &&
373          (IsConst0 || IsConst1 || V0->getType() == V1->getType()) &&
374          (ScalarTy->isIntegerTy() || ScalarTy->isFloatingPointTy()) &&
375          "Unexpected types for insert into binop");
376 
377   unsigned Opcode = I.getOpcode();
378   int ScalarOpCost, VectorOpCost;
379   if (IsCmp) {
380     ScalarOpCost = TTI.getCmpSelInstrCost(Opcode, ScalarTy);
381     VectorOpCost = TTI.getCmpSelInstrCost(Opcode, VecTy);
382   } else {
383     ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy);
384     VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy);
385   }
386 
387   // Get cost estimate for the insert element. This cost will factor into
388   // both sequences.
389   int InsertCost =
390       TTI.getVectorInstrCost(Instruction::InsertElement, VecTy, Index);
391   int OldCost = (IsConst0 ? 0 : InsertCost) + (IsConst1 ? 0 : InsertCost) +
392                 VectorOpCost;
393   int NewCost = ScalarOpCost + InsertCost +
394                 (IsConst0 ? 0 : !Ins0->hasOneUse() * InsertCost) +
395                 (IsConst1 ? 0 : !Ins1->hasOneUse() * InsertCost);
396 
397   // We want to scalarize unless the vector variant actually has lower cost.
398   if (OldCost < NewCost)
399     return false;
400 
401   // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index) -->
402   // inselt NewVecC, (scalar_op V0, V1), Index
403   if (IsCmp)
404     ++NumScalarCmp;
405   else
406     ++NumScalarBO;
407 
408   // For constant cases, extract the scalar element, this should constant fold.
409   IRBuilder<> Builder(&I);
410   if (IsConst0)
411     V0 = ConstantExpr::getExtractElement(VecC0, Builder.getInt64(Index));
412   if (IsConst1)
413     V1 = ConstantExpr::getExtractElement(VecC1, Builder.getInt64(Index));
414 
415   Value *Scalar =
416       IsCmp ? Opcode == Instruction::FCmp ? Builder.CreateFCmp(Pred, V0, V1)
417                                           : Builder.CreateICmp(Pred, V0, V1)
418             : Builder.CreateBinOp((Instruction::BinaryOps)Opcode, V0, V1);
419 
420   Scalar->setName(I.getName() + ".scalar");
421 
422   // All IR flags are safe to back-propagate. There is no potential for extra
423   // poison to be created by the scalar instruction.
424   if (auto *ScalarInst = dyn_cast<Instruction>(Scalar))
425     ScalarInst->copyIRFlags(&I);
426 
427   // Fold the vector constants in the original vectors into a new base vector.
428   Constant *NewVecC = IsCmp ? ConstantExpr::getCompare(Pred, VecC0, VecC1)
429                             : ConstantExpr::get(Opcode, VecC0, VecC1);
430   Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, Index);
431   I.replaceAllUsesWith(Insert);
432   Insert->takeName(&I);
433   return true;
434 }
435 
436 /// This is the entry point for all transforms. Pass manager differences are
437 /// handled in the callers of this function.
438 static bool runImpl(Function &F, const TargetTransformInfo &TTI,
439                     const DominatorTree &DT) {
440   if (DisableVectorCombine)
441     return false;
442 
443   bool MadeChange = false;
444   for (BasicBlock &BB : F) {
445     // Ignore unreachable basic blocks.
446     if (!DT.isReachableFromEntry(&BB))
447       continue;
448     // Do not delete instructions under here and invalidate the iterator.
449     // Walk the block forwards to enable simple iterative chains of transforms.
450     // TODO: It could be more efficient to remove dead instructions
451     //       iteratively in this loop rather than waiting until the end.
452     for (Instruction &I : BB) {
453       if (isa<DbgInfoIntrinsic>(I))
454         continue;
455       MadeChange |= foldExtractExtract(I, TTI);
456       MadeChange |= foldBitcastShuf(I, TTI);
457       MadeChange |= scalarizeBinopOrCmp(I, TTI);
458     }
459   }
460 
461   // We're done with transforms, so remove dead instructions.
462   if (MadeChange)
463     for (BasicBlock &BB : F)
464       SimplifyInstructionsInBlock(&BB);
465 
466   return MadeChange;
467 }
468 
469 // Pass manager boilerplate below here.
470 
471 namespace {
472 class VectorCombineLegacyPass : public FunctionPass {
473 public:
474   static char ID;
475   VectorCombineLegacyPass() : FunctionPass(ID) {
476     initializeVectorCombineLegacyPassPass(*PassRegistry::getPassRegistry());
477   }
478 
479   void getAnalysisUsage(AnalysisUsage &AU) const override {
480     AU.addRequired<DominatorTreeWrapperPass>();
481     AU.addRequired<TargetTransformInfoWrapperPass>();
482     AU.setPreservesCFG();
483     AU.addPreserved<DominatorTreeWrapperPass>();
484     AU.addPreserved<GlobalsAAWrapperPass>();
485     AU.addPreserved<AAResultsWrapperPass>();
486     AU.addPreserved<BasicAAWrapperPass>();
487     FunctionPass::getAnalysisUsage(AU);
488   }
489 
490   bool runOnFunction(Function &F) override {
491     if (skipFunction(F))
492       return false;
493     auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
494     auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
495     return runImpl(F, TTI, DT);
496   }
497 };
498 } // namespace
499 
500 char VectorCombineLegacyPass::ID = 0;
501 INITIALIZE_PASS_BEGIN(VectorCombineLegacyPass, "vector-combine",
502                       "Optimize scalar/vector ops", false,
503                       false)
504 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
505 INITIALIZE_PASS_END(VectorCombineLegacyPass, "vector-combine",
506                     "Optimize scalar/vector ops", false, false)
507 Pass *llvm::createVectorCombinePass() {
508   return new VectorCombineLegacyPass();
509 }
510 
511 PreservedAnalyses VectorCombinePass::run(Function &F,
512                                          FunctionAnalysisManager &FAM) {
513   TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F);
514   DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
515   if (!runImpl(F, TTI, DT))
516     return PreservedAnalyses::all();
517   PreservedAnalyses PA;
518   PA.preserveSet<CFGAnalyses>();
519   PA.preserve<GlobalsAA>();
520   PA.preserve<AAManager>();
521   PA.preserve<BasicAA>();
522   return PA;
523 }
524