[InstCombine] Signed saturation patterns
[llvm-core.git] / include / llvm / Transforms / Vectorize / LoopVectorize.h
blobd824e2903ef3767c2fa1289c71aa4c0510ca30e9
1 //===- LoopVectorize.h ------------------------------------------*- C++ -*-===//
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 is the LLVM loop vectorizer. This pass modifies 'vectorizable' loops
10 // and generates target-independent LLVM-IR.
11 // The vectorizer uses the TargetTransformInfo analysis to estimate the costs
12 // of instructions in order to estimate the profitability of vectorization.
14 // The loop vectorizer combines consecutive loop iterations into a single
15 // 'wide' iteration. After this transformation the index is incremented
16 // by the SIMD vector width, and not by one.
18 // This pass has three parts:
19 // 1. The main loop pass that drives the different parts.
20 // 2. LoopVectorizationLegality - A unit that checks for the legality
21 // of the vectorization.
22 // 3. InnerLoopVectorizer - A unit that performs the actual
23 // widening of instructions.
24 // 4. LoopVectorizationCostModel - A unit that checks for the profitability
25 // of vectorization. It decides on the optimal vector width, which
26 // can be one, if vectorization is not profitable.
28 // There is a development effort going on to migrate loop vectorizer to the
29 // VPlan infrastructure and to introduce outer loop vectorization support (see
30 // docs/Proposal/VectorizationPlan.rst and
31 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119523.html). For this
32 // purpose, we temporarily introduced the VPlan-native vectorization path: an
33 // alternative vectorization path that is natively implemented on top of the
34 // VPlan infrastructure. See EnableVPlanNativePath for enabling.
36 //===----------------------------------------------------------------------===//
38 // The reduction-variable vectorization is based on the paper:
39 // D. Nuzman and R. Henderson. Multi-platform Auto-vectorization.
41 // Variable uniformity checks are inspired by:
42 // Karrenberg, R. and Hack, S. Whole Function Vectorization.
44 // The interleaved access vectorization is based on the paper:
45 // Dorit Nuzman, Ira Rosen and Ayal Zaks. Auto-Vectorization of Interleaved
46 // Data for SIMD
48 // Other ideas/concepts are from:
49 // A. Zaks and D. Nuzman. Autovectorization in GCC-two years later.
51 // S. Maleki, Y. Gao, M. Garzaran, T. Wong and D. Padua. An Evaluation of
52 // Vectorizing Compilers.
54 //===----------------------------------------------------------------------===//
56 #ifndef LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H
57 #define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H
59 #include "llvm/Analysis/AliasAnalysis.h"
60 #include "llvm/IR/PassManager.h"
61 #include <functional>
63 namespace llvm {
65 class AssumptionCache;
66 class BlockFrequencyInfo;
67 class DemandedBits;
68 class DominatorTree;
69 class Function;
70 class Loop;
71 class LoopAccessInfo;
72 class LoopInfo;
73 class OptimizationRemarkEmitter;
74 class ProfileSummaryInfo;
75 class ScalarEvolution;
76 class TargetLibraryInfo;
77 class TargetTransformInfo;
79 extern cl::opt<bool> EnableLoopInterleaving;
80 extern cl::opt<bool> EnableLoopVectorization;
82 struct LoopVectorizeOptions {
83 /// If false, consider all loops for interleaving.
84 /// If true, only loops that explicitly request interleaving are considered.
85 bool InterleaveOnlyWhenForced;
87 /// If false, consider all loops for vectorization.
88 /// If true, only loops that explicitly request vectorization are considered.
89 bool VectorizeOnlyWhenForced;
91 /// The current defaults when creating the pass with no arguments are:
92 /// EnableLoopInterleaving = true and EnableLoopVectorization = true. This
93 /// means that interleaving default is consistent with the cl::opt flag, while
94 /// vectorization is not.
95 /// FIXME: The default for EnableLoopVectorization in the cl::opt should be
96 /// set to true, and the corresponding change to account for this be made in
97 /// opt.cpp. The initializations below will become:
98 /// InterleaveOnlyWhenForced(!EnableLoopInterleaving)
99 /// VectorizeOnlyWhenForced(!EnableLoopVectorization).
100 LoopVectorizeOptions()
101 : InterleaveOnlyWhenForced(false), VectorizeOnlyWhenForced(false) {}
102 LoopVectorizeOptions(bool InterleaveOnlyWhenForced,
103 bool VectorizeOnlyWhenForced)
104 : InterleaveOnlyWhenForced(InterleaveOnlyWhenForced),
105 VectorizeOnlyWhenForced(VectorizeOnlyWhenForced) {}
107 LoopVectorizeOptions &setInterleaveOnlyWhenForced(bool Value) {
108 InterleaveOnlyWhenForced = Value;
109 return *this;
112 LoopVectorizeOptions &setVectorizeOnlyWhenForced(bool Value) {
113 VectorizeOnlyWhenForced = Value;
114 return *this;
118 /// The LoopVectorize Pass.
119 struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> {
120 /// If false, consider all loops for interleaving.
121 /// If true, only loops that explicitly request interleaving are considered.
122 bool InterleaveOnlyWhenForced;
124 /// If false, consider all loops for vectorization.
125 /// If true, only loops that explicitly request vectorization are considered.
126 bool VectorizeOnlyWhenForced;
128 LoopVectorizePass(LoopVectorizeOptions Opts = {})
129 : InterleaveOnlyWhenForced(Opts.InterleaveOnlyWhenForced),
130 VectorizeOnlyWhenForced(Opts.VectorizeOnlyWhenForced) {}
132 ScalarEvolution *SE;
133 LoopInfo *LI;
134 TargetTransformInfo *TTI;
135 DominatorTree *DT;
136 BlockFrequencyInfo *BFI;
137 TargetLibraryInfo *TLI;
138 DemandedBits *DB;
139 AliasAnalysis *AA;
140 AssumptionCache *AC;
141 std::function<const LoopAccessInfo &(Loop &)> *GetLAA;
142 OptimizationRemarkEmitter *ORE;
143 ProfileSummaryInfo *PSI;
145 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
147 // Shim for old PM.
148 bool runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
149 TargetTransformInfo &TTI_, DominatorTree &DT_,
150 BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
151 DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_,
152 std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
153 OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_);
155 bool processLoop(Loop *L);
158 /// Reports a vectorization failure: print \p DebugMsg for debugging
159 /// purposes along with the corresponding optimization remark \p RemarkName.
160 /// If \p I is passed, it is an instruction that prevents vectorization.
161 /// Otherwise, the loop \p TheLoop is used for the location of the remark.
162 void reportVectorizationFailure(const StringRef DebugMsg,
163 const StringRef OREMsg, const StringRef ORETag,
164 OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr);
166 } // end namespace llvm
168 #endif // LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H