LLVM 24.0.0git
LoopVersioning.h
Go to the documentation of this file.
1//===- LoopVersioning.h - Utility to version a loop -------------*- 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 file defines a utility class to perform loop versioning. The versioned
10// loop speculates that otherwise may-aliasing memory accesses don't overlap and
11// emits checks to prove this.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
16#define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
17
18#include "llvm/IR/PassManager.h"
21
22namespace llvm {
23
24class Loop;
25class SCEVPredicate;
26class ScalarEvolution;
27class LoopAccessInfo;
28class LoopInfo;
31typedef std::pair<const RuntimeCheckingPtrGroup *,
34
35template <typename T> class ArrayRef;
36
37/// This class emits a version of the loop where run-time checks ensure
38/// that may-alias pointers can't overlap.
39///
40/// It currently only supports single-exit loops and assumes that the loop
41/// already has a preheader.
43public:
44 /// Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input.
45 /// It uses runtime check provided by the user. If \p UseLAIChecks is true,
46 /// we will retain the default checks made by LAI. Otherwise, construct an
47 /// object having no checks and we expect the user to add them.
51 MemorySSAUpdater *MSSAU = nullptr);
52
53 /// Performs the CFG manipulation part of versioning the loop including
54 /// the DominatorTree, LoopInfo and, if a MemorySSAUpdater was provided,
55 /// MemorySSA updates.
56 ///
57 /// The loop that was used to construct the class will be the "versioned" loop
58 /// i.e. the loop that will receive control if all the memchecks pass.
59 ///
60 /// This allows the loop transform pass to operate on the same loop regardless
61 /// of whether versioning was necessary or not:
62 ///
63 /// for each loop L:
64 /// analyze L
65 /// if versioning is necessary version L
66 /// transform L
68
69 /// Same but if the client has already precomputed the set of values
70 /// used outside the loop, this API will allows passing that.
71 LLVM_ABI void
72 versionLoop(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
73
74 /// Returns the versioned loop. Control flows here if pointers in the
75 /// loop don't alias (i.e. all memchecks passed). (This loop is actually the
76 /// same as the original loop that we got constructed with.)
77 Loop *getVersionedLoop() { return VersionedLoop; }
78
79 /// Returns the fall-back loop. Control flows here if pointers in the
80 /// loop may alias (i.e. one of the memchecks failed).
81 Loop *getNonVersionedLoop() { return NonVersionedLoop; }
82
83 /// Annotate memory instructions in the versioned loop with no-alias
84 /// metadata based on the memchecks issued.
85 ///
86 /// This is just wrapper that calls prepareNoAliasMetadata and
87 /// annotateInstWithNoAlias on the instructions of the versioned loop.
89
90 /// Returns a pair containing the alias_scope and noalias metadata nodes for
91 /// \p OrigInst, if they exists.
92 LLVM_ABI std::pair<MDNode *, MDNode *>
93 getNoAliasMetadataFor(const Instruction *OrigInst) const;
94
95 /// Set up the aliasing scopes based on the memchecks. This needs to
96 /// be called before the first call to annotateInstWithNoAlias.
98
99 /// Add the noalias annotations to \p VersionedInst.
100 ///
101 /// \p OrigInst is the instruction corresponding to \p VersionedInst in the
102 /// original loop. Initialize the aliasing scopes with
103 /// prepareNoAliasMetadata once before this can be called.
104 LLVM_ABI void annotateInstWithNoAlias(Instruction *VersionedInst,
105 const Instruction *OrigInst);
106
107private:
108 /// Adds the necessary PHI nodes for the versioned loops based on the
109 /// loop-defined values used outside of the loop.
110 ///
111 /// This needs to be called after versionLoop if there are defs in the loop
112 /// that are used outside the loop.
113 void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
114
115 /// Add the noalias annotations to \p I. Initialize the aliasing
116 /// scopes with prepareNoAliasMetadata once before this can be called.
119 }
120
121 /// The original loop. This becomes the "versioned" one. I.e.,
122 /// control flows here if pointers in the loop don't alias.
123 Loop *VersionedLoop;
124 /// The fall-back loop. I.e. control flows here if pointers in the
125 /// loop may alias (memchecks failed).
126 Loop *NonVersionedLoop = nullptr;
127
128 /// This maps the instructions from VersionedLoop to their counterpart
129 /// in NonVersionedLoop.
131
132 /// The set of alias checks that we are versioning for.
133 SmallVector<RuntimePointerCheck, 4> AliasChecks;
134
135 /// The set of SCEV checks that we are versioning for.
136 const SCEVPredicate &Preds;
137
138 /// Maps a pointer to the pointer checking group that the pointer
139 /// belongs to.
140 DenseMap<const Value *, const RuntimeCheckingPtrGroup *> PtrToGroup;
141
142 /// The alias scope corresponding to a pointer checking group.
143 DenseMap<const RuntimeCheckingPtrGroup *, MDNode *> GroupToScope;
144
145 /// The list of alias scopes that a pointer checking group can't alias.
146 DenseMap<const RuntimeCheckingPtrGroup *, MDNode *>
147 GroupToNonAliasingScopeList;
148
149 /// Analyses used.
150 const LoopAccessInfo &LAI;
151 LoopInfo *LI;
152 DominatorTree *DT;
153 ScalarEvolution *SE;
154
155 /// Updater to keep MemorySSA up to date, or nullptr if MemorySSA does not
156 /// need updating.
157 MemorySSAUpdater *MSSAU;
158};
159
160/// Expose LoopVersioning as a pass. Currently this is only used for
161/// unit-testing. It adds all memchecks necessary to remove all may-aliasing
162/// array accesses from the loop.
163class LoopVersioningPass : public OptionalPassInfoMixin<LoopVersioningPass> {
164public:
166};
167}
168
169#endif
#define LLVM_ABI
Definition Compiler.h:215
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
Drive the analysis of memory accesses in the loop.
Expose LoopVersioning as a pass.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
LLVM_ABI void annotateLoopWithNoAlias()
Annotate memory instructions in the versioned loop with no-alias metadata based on the memchecks issu...
Loop * getVersionedLoop()
Returns the versioned loop.
LLVM_ABI void prepareNoAliasMetadata()
Set up the aliasing scopes based on the memchecks.
LLVM_ABI void annotateInstWithNoAlias(Instruction *VersionedInst, const Instruction *OrigInst)
Add the noalias annotations to VersionedInst.
void versionLoop()
Performs the CFG manipulation part of versioning the loop including the DominatorTree,...
LLVM_ABI std::pair< MDNode *, MDNode * > getNoAliasMetadataFor(const Instruction *OrigInst) const
Returns a pair containing the alias_scope and noalias metadata nodes for OrigInst,...
LLVM_ABI LoopVersioning(const LoopAccessInfo &LAI, ArrayRef< RuntimePointerCheck > Checks, Loop *L, LoopInfo *LI, DominatorTree *DT, ScalarEvolution *SE, MemorySSAUpdater *MSSAU=nullptr)
Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input.
Loop * getNonVersionedLoop()
Returns the fall-back loop.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This class represents an assumption made using SCEV expressions which can be checked at run-time.
The main scalar evolution driver.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is an optimization pass for GlobalISel generic memory operations.
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
LLVM_ABI SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in for passes that can be skipped.