Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 1 | //===- LoopVersioningLICM.cpp - LICM Loop Versioning ----------------------===// |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // When alias analysis is uncertain about the aliasing between any two accesses, |
| 11 | // it will return MayAlias. This uncertainty from alias analysis restricts LICM |
| 12 | // from proceeding further. In cases where alias analysis is uncertain we might |
| 13 | // use loop versioning as an alternative. |
| 14 | // |
| 15 | // Loop Versioning will create a version of the loop with aggressive aliasing |
| 16 | // assumptions in addition to the original with conservative (default) aliasing |
| 17 | // assumptions. The version of the loop making aggressive aliasing assumptions |
| 18 | // will have all the memory accesses marked as no-alias. These two versions of |
| 19 | // loop will be preceded by a memory runtime check. This runtime check consists |
| 20 | // of bound checks for all unique memory accessed in loop, and it ensures the |
| 21 | // lack of memory aliasing. The result of the runtime check determines which of |
| 22 | // the loop versions is executed: If the runtime check detects any memory |
| 23 | // aliasing, then the original loop is executed. Otherwise, the version with |
| 24 | // aggressive aliasing assumptions is used. |
| 25 | // |
| 26 | // Following are the top level steps: |
| 27 | // |
| 28 | // a) Perform LoopVersioningLICM's feasibility check. |
| 29 | // b) If loop is a candidate for versioning then create a memory bound check, |
| 30 | // by considering all the memory accesses in loop body. |
| 31 | // c) Clone original loop and set all memory accesses as no-alias in new loop. |
| 32 | // d) Set original loop & versioned loop as a branch target of the runtime check |
| 33 | // result. |
| 34 | // |
| 35 | // It transforms loop as shown below: |
| 36 | // |
| 37 | // +----------------+ |
| 38 | // |Runtime Memcheck| |
| 39 | // +----------------+ |
| 40 | // | |
| 41 | // +----------+----------------+----------+ |
| 42 | // | | |
| 43 | // +---------+----------+ +-----------+----------+ |
| 44 | // |Orig Loop Preheader | |Cloned Loop Preheader | |
| 45 | // +--------------------+ +----------------------+ |
| 46 | // | | |
| 47 | // +--------------------+ +----------------------+ |
| 48 | // |Orig Loop Body | |Cloned Loop Body | |
| 49 | // +--------------------+ +----------------------+ |
| 50 | // | | |
| 51 | // +--------------------+ +----------------------+ |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 52 | // |Orig Loop Exit Block| |Cloned Loop Exit Block| |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 53 | // +--------------------+ +-----------+----------+ |
| 54 | // | | |
| 55 | // +----------+--------------+-----------+ |
| 56 | // | |
| 57 | // +-----+----+ |
| 58 | // |Join Block| |
| 59 | // +----------+ |
| 60 | // |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/SmallVector.h" |
| 64 | #include "llvm/ADT/StringRef.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 65 | #include "llvm/Analysis/AliasAnalysis.h" |
| 66 | #include "llvm/Analysis/AliasSetTracker.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 67 | #include "llvm/Analysis/GlobalsModRef.h" |
| 68 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
| 69 | #include "llvm/Analysis/LoopInfo.h" |
| 70 | #include "llvm/Analysis/LoopPass.h" |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/ScalarEvolution.h" |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 73 | #include "llvm/IR/CallSite.h" |
| 74 | #include "llvm/IR/Constants.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 75 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 76 | #include "llvm/IR/Instruction.h" |
| 77 | #include "llvm/IR/Instructions.h" |
| 78 | #include "llvm/IR/LLVMContext.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 79 | #include "llvm/IR/MDBuilder.h" |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 80 | #include "llvm/IR/Metadata.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 81 | #include "llvm/IR/Type.h" |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 82 | #include "llvm/IR/Value.h" |
| 83 | #include "llvm/Pass.h" |
| 84 | #include "llvm/Support/Casting.h" |
| 85 | #include "llvm/Support/CommandLine.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 86 | #include "llvm/Support/Debug.h" |
| 87 | #include "llvm/Support/raw_ostream.h" |
| 88 | #include "llvm/Transforms/Scalar.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 89 | #include "llvm/Transforms/Utils.h" |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 90 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 91 | #include "llvm/Transforms/Utils/LoopVersioning.h" |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 92 | #include <cassert> |
| 93 | #include <memory> |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 94 | |
| 95 | using namespace llvm; |
| 96 | |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 97 | #define DEBUG_TYPE "loop-versioning-licm" |
| 98 | |
| 99 | static const char *LICMVersioningMetaData = "llvm.loop.licm_versioning.disable"; |
| 100 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 101 | /// Threshold minimum allowed percentage for possible |
| 102 | /// invariant instructions in a loop. |
| 103 | static cl::opt<float> |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 104 | LVInvarThreshold("licm-versioning-invariant-threshold", |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 105 | cl::desc("LoopVersioningLICM's minimum allowed percentage" |
| 106 | "of possible invariant instructions per loop"), |
| 107 | cl::init(25), cl::Hidden); |
| 108 | |
| 109 | /// Threshold for maximum allowed loop nest/depth |
| 110 | static cl::opt<unsigned> LVLoopDepthThreshold( |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 111 | "licm-versioning-max-depth-threshold", |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 112 | cl::desc( |
| 113 | "LoopVersioningLICM's threshold for maximum allowed loop nest/depth"), |
| 114 | cl::init(2), cl::Hidden); |
| 115 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 116 | /// Create MDNode for input string. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 117 | static MDNode *createStringMetadata(Loop *TheLoop, StringRef Name, unsigned V) { |
| 118 | LLVMContext &Context = TheLoop->getHeader()->getContext(); |
| 119 | Metadata *MDs[] = { |
| 120 | MDString::get(Context, Name), |
| 121 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), V))}; |
| 122 | return MDNode::get(Context, MDs); |
| 123 | } |
| 124 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 125 | /// Set input string into loop metadata by keeping other values intact. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 126 | void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *MDString, |
| 127 | unsigned V) { |
| 128 | SmallVector<Metadata *, 4> MDs(1); |
| 129 | // If the loop already has metadata, retain it. |
| 130 | MDNode *LoopID = TheLoop->getLoopID(); |
| 131 | if (LoopID) { |
| 132 | for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { |
| 133 | MDNode *Node = cast<MDNode>(LoopID->getOperand(i)); |
| 134 | MDs.push_back(Node); |
| 135 | } |
| 136 | } |
| 137 | // Add new metadata. |
| 138 | MDs.push_back(createStringMetadata(TheLoop, MDString, V)); |
| 139 | // Replace current metadata node with new one. |
| 140 | LLVMContext &Context = TheLoop->getHeader()->getContext(); |
| 141 | MDNode *NewLoopID = MDNode::get(Context, MDs); |
| 142 | // Set operand 0 to refer to the loop id itself. |
| 143 | NewLoopID->replaceOperandWith(0, NewLoopID); |
| 144 | TheLoop->setLoopID(NewLoopID); |
| 145 | } |
| 146 | |
| 147 | namespace { |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 148 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 149 | struct LoopVersioningLICM : public LoopPass { |
| 150 | static char ID; |
| 151 | |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 152 | LoopVersioningLICM() |
| 153 | : LoopPass(ID), LoopDepthThreshold(LVLoopDepthThreshold), |
| 154 | InvariantThreshold(LVInvarThreshold) { |
| 155 | initializeLoopVersioningLICMPass(*PassRegistry::getPassRegistry()); |
| 156 | } |
| 157 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 158 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
| 159 | |
| 160 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 161 | AU.setPreservesCFG(); |
| 162 | AU.addRequired<AAResultsWrapperPass>(); |
| 163 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 164 | AU.addRequiredID(LCSSAID); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 165 | AU.addRequired<LoopAccessLegacyAnalysis>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 166 | AU.addRequired<LoopInfoWrapperPass>(); |
| 167 | AU.addRequiredID(LoopSimplifyID); |
| 168 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 169 | AU.addPreserved<AAResultsWrapperPass>(); |
| 170 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 171 | AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 174 | StringRef getPassName() const override { return "Loop Versioning for LICM"; } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 175 | |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 176 | void reset() { |
| 177 | AA = nullptr; |
| 178 | SE = nullptr; |
| 179 | LAA = nullptr; |
| 180 | CurLoop = nullptr; |
| 181 | LoadAndStoreCounter = 0; |
| 182 | InvariantCounter = 0; |
| 183 | IsReadOnlyLoop = true; |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 184 | ORE = nullptr; |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 185 | CurAST.reset(); |
| 186 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 187 | |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 188 | class AutoResetter { |
| 189 | public: |
| 190 | AutoResetter(LoopVersioningLICM &LVLICM) : LVLICM(LVLICM) {} |
| 191 | ~AutoResetter() { LVLICM.reset(); } |
| 192 | |
| 193 | private: |
| 194 | LoopVersioningLICM &LVLICM; |
| 195 | }; |
| 196 | |
| 197 | private: |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 198 | // Current AliasAnalysis information |
| 199 | AliasAnalysis *AA = nullptr; |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 200 | |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 201 | // Current ScalarEvolution |
| 202 | ScalarEvolution *SE = nullptr; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 203 | |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 204 | // Current LoopAccessAnalysis |
| 205 | LoopAccessLegacyAnalysis *LAA = nullptr; |
| 206 | |
| 207 | // Current Loop's LoopAccessInfo |
| 208 | const LoopAccessInfo *LAI = nullptr; |
| 209 | |
| 210 | // The current loop we are working on. |
| 211 | Loop *CurLoop = nullptr; |
| 212 | |
| 213 | // AliasSet information for the current loop. |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 214 | std::unique_ptr<AliasSetTracker> CurAST; |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 215 | |
| 216 | // Maximum loop nest threshold |
| 217 | unsigned LoopDepthThreshold; |
| 218 | |
| 219 | // Minimum invariant threshold |
| 220 | float InvariantThreshold; |
| 221 | |
| 222 | // Counter to track num of load & store |
| 223 | unsigned LoadAndStoreCounter = 0; |
| 224 | |
| 225 | // Counter to track num of invariant |
| 226 | unsigned InvariantCounter = 0; |
| 227 | |
| 228 | // Read only loop marker. |
| 229 | bool IsReadOnlyLoop = true; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 230 | |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 231 | // OptimizationRemarkEmitter |
| 232 | OptimizationRemarkEmitter *ORE; |
| 233 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 234 | bool isLegalForVersioning(); |
| 235 | bool legalLoopStructure(); |
| 236 | bool legalLoopInstructions(); |
| 237 | bool legalLoopMemoryAccesses(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 238 | bool isLoopAlreadyVisited(); |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 239 | void setNoAliasToLoop(Loop *VerLoop); |
| 240 | bool instructionSafeForVersioning(Instruction *I); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 241 | }; |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 242 | |
| 243 | } // end anonymous namespace |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 244 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 245 | /// Check loop structure and confirms it's good for LoopVersioningLICM. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 246 | bool LoopVersioningLICM::legalLoopStructure() { |
Florian Hahn | 2e03213 | 2016-12-19 17:13:37 +0000 | [diff] [blame] | 247 | // Loop must be in loop simplify form. |
| 248 | if (!CurLoop->isLoopSimplifyForm()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 249 | LLVM_DEBUG(dbgs() << " loop is not in loop-simplify form.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | // Loop should be innermost loop, if not return false. |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 253 | if (!CurLoop->getSubLoops().empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 254 | LLVM_DEBUG(dbgs() << " loop is not innermost\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | // Loop should have a single backedge, if not return false. |
| 258 | if (CurLoop->getNumBackEdges() != 1) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 259 | LLVM_DEBUG(dbgs() << " loop has multiple backedges\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | // Loop must have a single exiting block, if not return false. |
| 263 | if (!CurLoop->getExitingBlock()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 264 | LLVM_DEBUG(dbgs() << " loop has multiple exiting block\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | // We only handle bottom-tested loop, i.e. loop in which the condition is |
| 268 | // checked at the end of each iteration. With that we can assume that all |
| 269 | // instructions in the loop are executed the same number of times. |
| 270 | if (CurLoop->getExitingBlock() != CurLoop->getLoopLatch()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 271 | LLVM_DEBUG(dbgs() << " loop is not bottom tested\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | // Parallel loops must not have aliasing loop-invariant memory accesses. |
| 275 | // Hence we don't need to version anything in this case. |
| 276 | if (CurLoop->isAnnotatedParallel()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 277 | LLVM_DEBUG(dbgs() << " Parallel loop is not worth versioning\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | // Loop depth more then LoopDepthThreshold are not allowed |
| 281 | if (CurLoop->getLoopDepth() > LoopDepthThreshold) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 282 | LLVM_DEBUG(dbgs() << " loop depth is more then threshold\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 283 | return false; |
| 284 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 285 | // We need to be able to compute the loop trip count in order |
| 286 | // to generate the bound checks. |
| 287 | const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop); |
| 288 | if (ExitCount == SE->getCouldNotCompute()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 289 | LLVM_DEBUG(dbgs() << " loop does not has trip count\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 295 | /// Check memory accesses in loop and confirms it's good for |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 296 | /// LoopVersioningLICM. |
| 297 | bool LoopVersioningLICM::legalLoopMemoryAccesses() { |
| 298 | bool HasMayAlias = false; |
| 299 | bool TypeSafety = false; |
| 300 | bool HasMod = false; |
| 301 | // Memory check: |
| 302 | // Transform phase will generate a versioned loop and also a runtime check to |
| 303 | // ensure the pointers are independent and they don’t alias. |
| 304 | // In version variant of loop, alias meta data asserts that all access are |
| 305 | // mutually independent. |
| 306 | // |
| 307 | // Pointers aliasing in alias domain are avoided because with multiple |
| 308 | // aliasing domains we may not be able to hoist potential loop invariant |
| 309 | // access out of the loop. |
| 310 | // |
| 311 | // Iterate over alias tracker sets, and confirm AliasSets doesn't have any |
| 312 | // must alias set. |
| 313 | for (const auto &I : *CurAST) { |
| 314 | const AliasSet &AS = I; |
| 315 | // Skip Forward Alias Sets, as this should be ignored as part of |
| 316 | // the AliasSetTracker object. |
| 317 | if (AS.isForwardingAliasSet()) |
| 318 | continue; |
| 319 | // With MustAlias its not worth adding runtime bound check. |
| 320 | if (AS.isMustAlias()) |
| 321 | return false; |
| 322 | Value *SomePtr = AS.begin()->getValue(); |
| 323 | bool TypeCheck = true; |
| 324 | // Check for Mod & MayAlias |
| 325 | HasMayAlias |= AS.isMayAlias(); |
| 326 | HasMod |= AS.isMod(); |
| 327 | for (const auto &A : AS) { |
| 328 | Value *Ptr = A.getValue(); |
| 329 | // Alias tracker should have pointers of same data type. |
| 330 | TypeCheck = (TypeCheck && (SomePtr->getType() == Ptr->getType())); |
| 331 | } |
| 332 | // At least one alias tracker should have pointers of same data type. |
| 333 | TypeSafety |= TypeCheck; |
| 334 | } |
| 335 | // Ensure types should be of same type. |
| 336 | if (!TypeSafety) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 337 | LLVM_DEBUG(dbgs() << " Alias tracker type safety failed!\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | // Ensure loop body shouldn't be read only. |
| 341 | if (!HasMod) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 342 | LLVM_DEBUG(dbgs() << " No memory modified in loop body\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | // Make sure alias set has may alias case. |
| 346 | // If there no alias memory ambiguity, return false. |
| 347 | if (!HasMayAlias) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 348 | LLVM_DEBUG(dbgs() << " No ambiguity in memory access.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 354 | /// Check loop instructions safe for Loop versioning. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 355 | /// It returns true if it's safe else returns false. |
| 356 | /// Consider following: |
| 357 | /// 1) Check all load store in loop body are non atomic & non volatile. |
| 358 | /// 2) Check function call safety, by ensuring its not accessing memory. |
| 359 | /// 3) Loop body shouldn't have any may throw instruction. |
| 360 | bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) { |
| 361 | assert(I != nullptr && "Null instruction found!"); |
| 362 | // Check function call safety |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 363 | if (isa<CallInst>(I) && !AA->doesNotAccessMemory(CallSite(I))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 364 | LLVM_DEBUG(dbgs() << " Unsafe call site found.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | // Avoid loops with possiblity of throw |
| 368 | if (I->mayThrow()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 369 | LLVM_DEBUG(dbgs() << " May throw instruction found in loop body\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | // If current instruction is load instructions |
| 373 | // make sure it's a simple load (non atomic & non volatile) |
| 374 | if (I->mayReadFromMemory()) { |
| 375 | LoadInst *Ld = dyn_cast<LoadInst>(I); |
| 376 | if (!Ld || !Ld->isSimple()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 377 | LLVM_DEBUG(dbgs() << " Found a non-simple load.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | LoadAndStoreCounter++; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 381 | Value *Ptr = Ld->getPointerOperand(); |
| 382 | // Check loop invariant. |
| 383 | if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) |
| 384 | InvariantCounter++; |
| 385 | } |
| 386 | // If current instruction is store instruction |
| 387 | // make sure it's a simple store (non atomic & non volatile) |
| 388 | else if (I->mayWriteToMemory()) { |
| 389 | StoreInst *St = dyn_cast<StoreInst>(I); |
| 390 | if (!St || !St->isSimple()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 391 | LLVM_DEBUG(dbgs() << " Found a non-simple store.\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | LoadAndStoreCounter++; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 395 | Value *Ptr = St->getPointerOperand(); |
| 396 | // Check loop invariant. |
| 397 | if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop)) |
| 398 | InvariantCounter++; |
| 399 | |
| 400 | IsReadOnlyLoop = false; |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 405 | /// Check loop instructions and confirms it's good for |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 406 | /// LoopVersioningLICM. |
| 407 | bool LoopVersioningLICM::legalLoopInstructions() { |
| 408 | // Resetting counters. |
| 409 | LoadAndStoreCounter = 0; |
| 410 | InvariantCounter = 0; |
| 411 | IsReadOnlyLoop = true; |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 412 | using namespace ore; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 413 | // Iterate over loop blocks and instructions of each block and check |
| 414 | // instruction safety. |
| 415 | for (auto *Block : CurLoop->getBlocks()) |
| 416 | for (auto &Inst : *Block) { |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 417 | // If instruction is unsafe just return false. |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 418 | if (!instructionSafeForVersioning(&Inst)) { |
| 419 | ORE->emit([&]() { |
| 420 | return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopInst", &Inst) |
| 421 | << " Unsafe Loop Instruction"; |
| 422 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 423 | return false; |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 424 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 425 | } |
| 426 | // Get LoopAccessInfo from current loop. |
Adam Nemet | a9f09c6 | 2016-06-17 22:35:41 +0000 | [diff] [blame] | 427 | LAI = &LAA->getInfo(CurLoop); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 428 | // Check LoopAccessInfo for need of runtime check. |
| 429 | if (LAI->getRuntimePointerChecking()->getChecks().empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 430 | LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | // Number of runtime-checks should be less then RuntimeMemoryCheckThreshold |
| 434 | if (LAI->getNumRuntimePointerChecks() > |
| 435 | VectorizerParams::RuntimeMemoryCheckThreshold) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 436 | LLVM_DEBUG( |
| 437 | dbgs() << " LAA: Runtime checks are more than threshold !!\n"); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 438 | ORE->emit([&]() { |
| 439 | return OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeCheck", |
| 440 | CurLoop->getStartLoc(), |
| 441 | CurLoop->getHeader()) |
| 442 | << "Number of runtime checks " |
| 443 | << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks()) |
| 444 | << " exceeds threshold " |
| 445 | << NV("Threshold", VectorizerParams::RuntimeMemoryCheckThreshold); |
| 446 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | // Loop should have at least one invariant load or store instruction. |
| 450 | if (!InvariantCounter) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 451 | LLVM_DEBUG(dbgs() << " Invariant not found !!\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | // Read only loop not allowed. |
| 455 | if (IsReadOnlyLoop) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 456 | LLVM_DEBUG(dbgs() << " Found a read-only loop!\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | // Profitablity check: |
| 460 | // Check invariant threshold, should be in limit. |
| 461 | if (InvariantCounter * 100 < InvariantThreshold * LoadAndStoreCounter) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 462 | LLVM_DEBUG( |
| 463 | dbgs() |
| 464 | << " Invariant load & store are less then defined threshold\n"); |
| 465 | LLVM_DEBUG(dbgs() << " Invariant loads & stores: " |
| 466 | << ((InvariantCounter * 100) / LoadAndStoreCounter) |
| 467 | << "%\n"); |
| 468 | LLVM_DEBUG(dbgs() << " Invariant loads & store threshold: " |
| 469 | << InvariantThreshold << "%\n"); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 470 | ORE->emit([&]() { |
| 471 | return OptimizationRemarkMissed(DEBUG_TYPE, "InvariantThreshold", |
| 472 | CurLoop->getStartLoc(), |
| 473 | CurLoop->getHeader()) |
| 474 | << "Invariant load & store " |
| 475 | << NV("LoadAndStoreCounter", |
| 476 | ((InvariantCounter * 100) / LoadAndStoreCounter)) |
| 477 | << " are less then defined threshold " |
| 478 | << NV("Threshold", InvariantThreshold); |
| 479 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 480 | return false; |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 485 | /// It checks loop is already visited or not. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 486 | /// check loop meta data, if loop revisited return true |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 487 | /// else false. |
| 488 | bool LoopVersioningLICM::isLoopAlreadyVisited() { |
| 489 | // Check LoopVersioningLICM metadata into loop |
Adam Nemet | f787826 | 2016-04-21 17:33:12 +0000 | [diff] [blame] | 490 | if (findStringMetadataForLoop(CurLoop, LICMVersioningMetaData)) { |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 496 | /// Checks legality for LoopVersioningLICM by considering following: |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 497 | /// a) loop structure legality b) loop instruction legality |
| 498 | /// c) loop memory access legality. |
| 499 | /// Return true if legal else returns false. |
| 500 | bool LoopVersioningLICM::isLegalForVersioning() { |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 501 | using namespace ore; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 502 | LLVM_DEBUG(dbgs() << "Loop: " << *CurLoop); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 503 | // Make sure not re-visiting same loop again. |
| 504 | if (isLoopAlreadyVisited()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 505 | LLVM_DEBUG( |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 506 | dbgs() << " Revisiting loop in LoopVersioningLICM not allowed.\n\n"); |
| 507 | return false; |
| 508 | } |
| 509 | // Check loop structure leagality. |
| 510 | if (!legalLoopStructure()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 511 | LLVM_DEBUG( |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 512 | dbgs() << " Loop structure not suitable for LoopVersioningLICM\n\n"); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 513 | ORE->emit([&]() { |
| 514 | return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopStruct", |
| 515 | CurLoop->getStartLoc(), |
| 516 | CurLoop->getHeader()) |
| 517 | << " Unsafe Loop structure"; |
| 518 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 519 | return false; |
| 520 | } |
| 521 | // Check loop instruction leagality. |
| 522 | if (!legalLoopInstructions()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 523 | LLVM_DEBUG( |
| 524 | dbgs() |
| 525 | << " Loop instructions not suitable for LoopVersioningLICM\n\n"); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 526 | return false; |
| 527 | } |
| 528 | // Check loop memory access leagality. |
| 529 | if (!legalLoopMemoryAccesses()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 530 | LLVM_DEBUG( |
| 531 | dbgs() |
| 532 | << " Loop memory access not suitable for LoopVersioningLICM\n\n"); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 533 | ORE->emit([&]() { |
| 534 | return OptimizationRemarkMissed(DEBUG_TYPE, "IllegalLoopMemoryAccess", |
| 535 | CurLoop->getStartLoc(), |
| 536 | CurLoop->getHeader()) |
| 537 | << " Unsafe Loop memory access"; |
| 538 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | // Loop versioning is feasible, return true. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame^] | 542 | LLVM_DEBUG(dbgs() << " Loop Versioning found to be beneficial\n\n"); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 543 | ORE->emit([&]() { |
| 544 | return OptimizationRemark(DEBUG_TYPE, "IsLegalForVersioning", |
| 545 | CurLoop->getStartLoc(), CurLoop->getHeader()) |
| 546 | << " Versioned loop for LICM." |
| 547 | << " Number of runtime checks we had to insert " |
| 548 | << NV("RuntimeChecks", LAI->getNumRuntimePointerChecks()); |
| 549 | }); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 550 | return true; |
| 551 | } |
| 552 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 553 | /// Update loop with aggressive aliasing assumptions. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 554 | /// It marks no-alias to any pairs of memory operations by assuming |
| 555 | /// loop should not have any must-alias memory accesses pairs. |
| 556 | /// During LoopVersioningLICM legality we ignore loops having must |
| 557 | /// aliasing memory accesses. |
| 558 | void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) { |
| 559 | // Get latch terminator instruction. |
| 560 | Instruction *I = VerLoop->getLoopLatch()->getTerminator(); |
| 561 | // Create alias scope domain. |
| 562 | MDBuilder MDB(I->getContext()); |
| 563 | MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain"); |
| 564 | StringRef Name = "LVAliasScope"; |
| 565 | SmallVector<Metadata *, 4> Scopes, NoAliases; |
| 566 | MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name); |
| 567 | // Iterate over each instruction of loop. |
| 568 | // set no-alias for all load & store instructions. |
| 569 | for (auto *Block : CurLoop->getBlocks()) { |
| 570 | for (auto &Inst : *Block) { |
| 571 | // Only interested in instruction that may modify or read memory. |
| 572 | if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory()) |
| 573 | continue; |
| 574 | Scopes.push_back(NewScope); |
| 575 | NoAliases.push_back(NewScope); |
| 576 | // Set no-alias for current instruction. |
| 577 | Inst.setMetadata( |
| 578 | LLVMContext::MD_noalias, |
| 579 | MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias), |
| 580 | MDNode::get(Inst.getContext(), NoAliases))); |
| 581 | // set alias-scope for current instruction. |
| 582 | Inst.setMetadata( |
| 583 | LLVMContext::MD_alias_scope, |
| 584 | MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope), |
| 585 | MDNode::get(Inst.getContext(), Scopes))); |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | bool LoopVersioningLICM::runOnLoop(Loop *L, LPPassManager &LPM) { |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 591 | // This will automatically release all resources hold by the current |
| 592 | // LoopVersioningLICM object. |
| 593 | AutoResetter Resetter(*this); |
| 594 | |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 595 | if (skipLoop(L)) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 596 | return false; |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 597 | // Get Analysis information. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 598 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| 599 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 600 | LAA = &getAnalysis<LoopAccessLegacyAnalysis>(); |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 601 | ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 602 | LAI = nullptr; |
| 603 | // Set Current Loop |
| 604 | CurLoop = L; |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 605 | CurAST.reset(new AliasSetTracker(*AA)); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 606 | |
| 607 | // Loop over the body of this loop, construct AST. |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 608 | LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 609 | for (auto *Block : L->getBlocks()) { |
| 610 | if (LI->getLoopFor(Block) == L) // Ignore blocks in subloop. |
| 611 | CurAST->add(*Block); // Incorporate the specified basic block |
| 612 | } |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 613 | |
| 614 | bool Changed = false; |
| 615 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 616 | // Check feasiblity of LoopVersioningLICM. |
| 617 | // If versioning found to be feasible and beneficial then proceed |
| 618 | // else simply return, by cleaning up memory. |
| 619 | if (isLegalForVersioning()) { |
| 620 | // Do loop versioning. |
| 621 | // Create memcheck for memory accessed inside loop. |
| 622 | // Clone original loop, and set blocks properly. |
Evgeny Astigeevich | 48fd87e | 2016-10-14 23:00:36 +0000 | [diff] [blame] | 623 | DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 624 | LoopVersioning LVer(*LAI, CurLoop, LI, DT, SE, true); |
| 625 | LVer.versionLoop(); |
| 626 | // Set Loop Versioning metaData for original loop. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 627 | addStringMetadataToLoop(LVer.getNonVersionedLoop(), LICMVersioningMetaData); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 628 | // Set Loop Versioning metaData for version loop. |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 629 | addStringMetadataToLoop(LVer.getVersionedLoop(), LICMVersioningMetaData); |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 630 | // Set "llvm.mem.parallel_loop_access" metaData to versioned loop. |
| 631 | addStringMetadataToLoop(LVer.getVersionedLoop(), |
| 632 | "llvm.mem.parallel_loop_access"); |
| 633 | // Update version loop with aggressive aliasing assumption. |
| 634 | setNoAliasToLoop(LVer.getVersionedLoop()); |
| 635 | Changed = true; |
| 636 | } |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 637 | return Changed; |
| 638 | } |
| 639 | |
| 640 | char LoopVersioningLICM::ID = 0; |
Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 641 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 642 | INITIALIZE_PASS_BEGIN(LoopVersioningLICM, "loop-versioning-licm", |
| 643 | "Loop Versioning For LICM", false, false) |
| 644 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| 645 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 646 | INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 647 | INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass) |
Xinliang David Li | 7853c1d | 2016-07-08 20:55:26 +0000 | [diff] [blame] | 648 | INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 649 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 650 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 651 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Ashutosh Nema | 007b425 | 2018-01-23 09:47:28 +0000 | [diff] [blame] | 652 | INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 653 | INITIALIZE_PASS_END(LoopVersioningLICM, "loop-versioning-licm", |
| 654 | "Loop Versioning For LICM", false, false) |
| 655 | |
| 656 | Pass *llvm::createLoopVersioningLICMPass() { return new LoopVersioningLICM(); } |