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. 13 // 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. 17 // 18 // This pass has four 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. 27 // 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/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. 35 // 36 //===----------------------------------------------------------------------===// 37 // 38 // The reduction-variable vectorization is based on the paper: 39 // D. Nuzman and R. Henderson. Multi-platform Auto-vectorization. 40 // 41 // Variable uniformity checks are inspired by: 42 // Karrenberg, R. and Hack, S. Whole Function Vectorization. 43 // 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 47 // 48 // Other ideas/concepts are from: 49 // A. Zaks and D. Nuzman. Autovectorization in GCC-two years later. 50 // 51 // S. Maleki, Y. Gao, M. Garzaran, T. Wong and D. Padua. An Evaluation of 52 // Vectorizing Compilers. 53 // 54 //===----------------------------------------------------------------------===// 55 56 #ifndef LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H 57 #define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H 58 59 #include "llvm/IR/PassManager.h" 60 #include "llvm/Support/CommandLine.h" 61 #include "llvm/Transforms/Utils/ExtraPassManager.h" 62 #include <functional> 63 64 namespace llvm { 65 66 class AssumptionCache; 67 class BlockFrequencyInfo; 68 class DemandedBits; 69 class DominatorTree; 70 class Function; 71 class Instruction; 72 class Loop; 73 class LoopAccessInfoManager; 74 class LoopInfo; 75 class OptimizationRemarkEmitter; 76 class ProfileSummaryInfo; 77 class ScalarEvolution; 78 class TargetLibraryInfo; 79 class TargetTransformInfo; 80 81 extern cl::opt<bool> EnableLoopInterleaving; 82 extern cl::opt<bool> EnableLoopVectorization; 83 84 struct LoopVectorizeOptions { 85 /// If false, consider all loops for interleaving. 86 /// If true, only loops that explicitly request interleaving are considered. 87 bool InterleaveOnlyWhenForced; 88 89 /// If false, consider all loops for vectorization. 90 /// If true, only loops that explicitly request vectorization are considered. 91 bool VectorizeOnlyWhenForced; 92 93 /// The current defaults when creating the pass with no arguments are: 94 /// EnableLoopInterleaving = true and EnableLoopVectorization = true. This 95 /// means that interleaving default is consistent with the cl::opt flag, while 96 /// vectorization is not. 97 /// FIXME: The default for EnableLoopVectorization in the cl::opt should be 98 /// set to true, and the corresponding change to account for this be made in 99 /// opt.cpp. The initializations below will become: 100 /// InterleaveOnlyWhenForced(!EnableLoopInterleaving) 101 /// VectorizeOnlyWhenForced(!EnableLoopVectorization). 102 LoopVectorizeOptions() 103 : InterleaveOnlyWhenForced(false), VectorizeOnlyWhenForced(false) {} 104 LoopVectorizeOptions(bool InterleaveOnlyWhenForced, 105 bool VectorizeOnlyWhenForced) 106 : InterleaveOnlyWhenForced(InterleaveOnlyWhenForced), 107 VectorizeOnlyWhenForced(VectorizeOnlyWhenForced) {} 108 109 LoopVectorizeOptions &setInterleaveOnlyWhenForced(bool Value) { 110 InterleaveOnlyWhenForced = Value; 111 return *this; 112 } 113 114 LoopVectorizeOptions &setVectorizeOnlyWhenForced(bool Value) { 115 VectorizeOnlyWhenForced = Value; 116 return *this; 117 } 118 }; 119 120 /// Storage for information about made changes. 121 struct LoopVectorizeResult { 122 bool MadeAnyChange; 123 bool MadeCFGChange; 124 125 LoopVectorizeResult(bool MadeAnyChange, bool MadeCFGChange) 126 : MadeAnyChange(MadeAnyChange), MadeCFGChange(MadeCFGChange) {} 127 }; 128 129 /// The LoopVectorize Pass. 130 struct LoopVectorizePass : public PassInfoMixin<LoopVectorizePass> { 131 private: 132 /// If false, consider all loops for interleaving. 133 /// If true, only loops that explicitly request interleaving are considered. 134 bool InterleaveOnlyWhenForced; 135 136 /// If false, consider all loops for vectorization. 137 /// If true, only loops that explicitly request vectorization are considered. 138 bool VectorizeOnlyWhenForced; 139 140 public: 141 LoopVectorizePass(LoopVectorizeOptions Opts = {}); 142 143 ScalarEvolution *SE; 144 LoopInfo *LI; 145 TargetTransformInfo *TTI; 146 DominatorTree *DT; 147 BlockFrequencyInfo *BFI; 148 TargetLibraryInfo *TLI; 149 DemandedBits *DB; 150 AssumptionCache *AC; 151 LoopAccessInfoManager *LAIs; 152 OptimizationRemarkEmitter *ORE; 153 ProfileSummaryInfo *PSI; 154 155 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 156 void printPipeline(raw_ostream &OS, 157 function_ref<StringRef(StringRef)> MapClassName2PassName); 158 159 // Shim for old PM. 160 LoopVectorizeResult runImpl(Function &F); 161 162 bool processLoop(Loop *L); 163 }; 164 165 /// Reports a vectorization failure: print \p DebugMsg for debugging 166 /// purposes along with the corresponding optimization remark \p RemarkName. 167 /// If \p I is passed, it is an instruction that prevents vectorization. 168 /// Otherwise, the loop \p TheLoop is used for the location of the remark. 169 void reportVectorizationFailure(const StringRef DebugMsg, 170 const StringRef OREMsg, const StringRef ORETag, 171 OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I = nullptr); 172 173 /// Same as above, but the debug message and optimization remark are identical 174 inline void reportVectorizationFailure(const StringRef DebugMsg, 175 const StringRef ORETag, 176 OptimizationRemarkEmitter *ORE, 177 Loop *TheLoop, 178 Instruction *I = nullptr) { 179 reportVectorizationFailure(DebugMsg, DebugMsg, ORETag, ORE, TheLoop, I); 180 } 181 182 /// A marker analysis to determine if extra passes should be run after loop 183 /// vectorization. 184 struct ShouldRunExtraVectorPasses 185 : public ShouldRunExtraPasses<ShouldRunExtraVectorPasses>, 186 public AnalysisInfoMixin<ShouldRunExtraVectorPasses> { 187 static AnalysisKey Key; 188 }; 189 } // end namespace llvm 190 191 #endif // LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZE_H 192