|
|
|||
File indexing completed on 2026-05-10 08:44:43
0001 //===- LoopVersioning.h - Utility to version a loop -------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file defines a utility class to perform loop versioning. The versioned 0010 // loop speculates that otherwise may-aliasing memory accesses don't overlap and 0011 // emits checks to prove this. 0012 // 0013 //===----------------------------------------------------------------------===// 0014 0015 #ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H 0016 #define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H 0017 0018 #include "llvm/IR/PassManager.h" 0019 #include "llvm/Transforms/Utils/LoopUtils.h" 0020 #include "llvm/Transforms/Utils/ValueMapper.h" 0021 0022 namespace llvm { 0023 0024 class Loop; 0025 class SCEVPredicate; 0026 class ScalarEvolution; 0027 class LoopAccessInfo; 0028 class LoopInfo; 0029 struct RuntimeCheckingPtrGroup; 0030 typedef std::pair<const RuntimeCheckingPtrGroup *, 0031 const RuntimeCheckingPtrGroup *> 0032 RuntimePointerCheck; 0033 0034 template <typename T> class ArrayRef; 0035 0036 /// This class emits a version of the loop where run-time checks ensure 0037 /// that may-alias pointers can't overlap. 0038 /// 0039 /// It currently only supports single-exit loops and assumes that the loop 0040 /// already has a preheader. 0041 class LoopVersioning { 0042 public: 0043 /// Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input. 0044 /// It uses runtime check provided by the user. If \p UseLAIChecks is true, 0045 /// we will retain the default checks made by LAI. Otherwise, construct an 0046 /// object having no checks and we expect the user to add them. 0047 LoopVersioning(const LoopAccessInfo &LAI, 0048 ArrayRef<RuntimePointerCheck> Checks, Loop *L, LoopInfo *LI, 0049 DominatorTree *DT, ScalarEvolution *SE); 0050 0051 /// Performs the CFG manipulation part of versioning the loop including 0052 /// the DominatorTree and LoopInfo updates. 0053 /// 0054 /// The loop that was used to construct the class will be the "versioned" loop 0055 /// i.e. the loop that will receive control if all the memchecks pass. 0056 /// 0057 /// This allows the loop transform pass to operate on the same loop regardless 0058 /// of whether versioning was necessary or not: 0059 /// 0060 /// for each loop L: 0061 /// analyze L 0062 /// if versioning is necessary version L 0063 /// transform L 0064 void versionLoop() { versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop)); } 0065 0066 /// Same but if the client has already precomputed the set of values 0067 /// used outside the loop, this API will allows passing that. 0068 void versionLoop(const SmallVectorImpl<Instruction *> &DefsUsedOutside); 0069 0070 /// Returns the versioned loop. Control flows here if pointers in the 0071 /// loop don't alias (i.e. all memchecks passed). (This loop is actually the 0072 /// same as the original loop that we got constructed with.) 0073 Loop *getVersionedLoop() { return VersionedLoop; } 0074 0075 /// Returns the fall-back loop. Control flows here if pointers in the 0076 /// loop may alias (i.e. one of the memchecks failed). 0077 Loop *getNonVersionedLoop() { return NonVersionedLoop; } 0078 0079 /// Annotate memory instructions in the versioned loop with no-alias 0080 /// metadata based on the memchecks issued. 0081 /// 0082 /// This is just wrapper that calls prepareNoAliasMetadata and 0083 /// annotateInstWithNoAlias on the instructions of the versioned loop. 0084 void annotateLoopWithNoAlias(); 0085 0086 /// Set up the aliasing scopes based on the memchecks. This needs to 0087 /// be called before the first call to annotateInstWithNoAlias. 0088 void prepareNoAliasMetadata(); 0089 0090 /// Add the noalias annotations to \p VersionedInst. 0091 /// 0092 /// \p OrigInst is the instruction corresponding to \p VersionedInst in the 0093 /// original loop. Initialize the aliasing scopes with 0094 /// prepareNoAliasMetadata once before this can be called. 0095 void annotateInstWithNoAlias(Instruction *VersionedInst, 0096 const Instruction *OrigInst); 0097 0098 private: 0099 /// Adds the necessary PHI nodes for the versioned loops based on the 0100 /// loop-defined values used outside of the loop. 0101 /// 0102 /// This needs to be called after versionLoop if there are defs in the loop 0103 /// that are used outside the loop. 0104 void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside); 0105 0106 /// Add the noalias annotations to \p I. Initialize the aliasing 0107 /// scopes with prepareNoAliasMetadata once before this can be called. 0108 void annotateInstWithNoAlias(Instruction *I) { 0109 annotateInstWithNoAlias(I, I); 0110 } 0111 0112 /// The original loop. This becomes the "versioned" one. I.e., 0113 /// control flows here if pointers in the loop don't alias. 0114 Loop *VersionedLoop; 0115 /// The fall-back loop. I.e. control flows here if pointers in the 0116 /// loop may alias (memchecks failed). 0117 Loop *NonVersionedLoop = nullptr; 0118 0119 /// This maps the instructions from VersionedLoop to their counterpart 0120 /// in NonVersionedLoop. 0121 ValueToValueMapTy VMap; 0122 0123 /// The set of alias checks that we are versioning for. 0124 SmallVector<RuntimePointerCheck, 4> AliasChecks; 0125 0126 /// The set of SCEV checks that we are versioning for. 0127 const SCEVPredicate &Preds; 0128 0129 /// Maps a pointer to the pointer checking group that the pointer 0130 /// belongs to. 0131 DenseMap<const Value *, const RuntimeCheckingPtrGroup *> PtrToGroup; 0132 0133 /// The alias scope corresponding to a pointer checking group. 0134 DenseMap<const RuntimeCheckingPtrGroup *, MDNode *> GroupToScope; 0135 0136 /// The list of alias scopes that a pointer checking group can't alias. 0137 DenseMap<const RuntimeCheckingPtrGroup *, MDNode *> 0138 GroupToNonAliasingScopeList; 0139 0140 /// Analyses used. 0141 const LoopAccessInfo &LAI; 0142 LoopInfo *LI; 0143 DominatorTree *DT; 0144 ScalarEvolution *SE; 0145 }; 0146 0147 /// Expose LoopVersioning as a pass. Currently this is only used for 0148 /// unit-testing. It adds all memchecks necessary to remove all may-aliasing 0149 /// array accesses from the loop. 0150 class LoopVersioningPass : public PassInfoMixin<LoopVersioningPass> { 0151 public: 0152 PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); 0153 }; 0154 } 0155 0156 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|