33#define DEBUG_TYPE "loop-versioning"
38 cl::desc(
"Add no-alias annotation for instructions that "
39 "are disambiguated by memchecks"));
45 : VersionedLoop(L), AliasChecks(Checks), Preds(LAI.getPSE().getPredicate()),
46 LAI(LAI), LI(LI), DT(DT), SE(SE) {}
52 "Loop is not in loop-simplify form");
54 Value *MemRuntimeCheck;
55 Value *SCEVRuntimeCheck;
56 Value *RuntimeCheck =
nullptr;
66 VersionedLoop, AliasChecks, Exp2);
71 Exp.expandCodeForPredicate(&Preds, RuntimeCheckBB->
getTerminator());
76 if (MemRuntimeCheck && SCEVRuntimeCheck) {
79 Builder.
CreateOr(MemRuntimeCheck, SCEVRuntimeCheck,
"lver.safe");
81 RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
83 assert(RuntimeCheck &&
"called even though we don't need "
84 "any runtime checks");
103 ".lver.orig", LI, DT, NonVersionedLoopBlocks);
119 addPHINodes(DefsUsedOutside);
124 "The versioned loops should be in simplify form.");
127void LoopVersioning::addPHINodes(
130 assert(PHIBlock &&
"No single successor to loop exit block");
135 for (
auto *Inst : DefsUsedOutside) {
138 for (
auto I = PHIBlock->
begin(); (PN = dyn_cast<PHINode>(
I)); ++
I) {
150 if (!VersionedLoop->
contains(cast<Instruction>(U)->getParent()))
152 for (
User *U : UsersToUpdate)
153 U->replaceUsesOfWith(Inst, PN);
159 for (
auto I = PHIBlock->
begin(); (PN = dyn_cast<PHINode>(
I)); ++
I) {
161 "Exit block should only have on predecessor");
165 auto Mapped = VMap.
find(ClonedValue);
166 if (Mapped != VMap.
end())
167 ClonedValue = Mapped->second;
195 for (
unsigned PtrIdx : Group.Members)
202 GroupToNonAliasingScopes;
204 for (
const auto &
Check : AliasChecks)
205 GroupToNonAliasingScopes[
Check.first].push_back(GroupToScope[
Check.second]);
210 for (
const auto &Pair : GroupToNonAliasingScopes)
211 GroupToNonAliasingScopeList[Pair.first] =
MDNode::get(Context, Pair.second);
233 const Value *
Ptr = isa<LoadInst>(OrigInst)
234 ? cast<LoadInst>(OrigInst)->getPointerOperand()
235 : cast<StoreInst>(OrigInst)->getPointerOperand();
238 auto Group = PtrToGroup.find(
Ptr);
239 if (Group != PtrToGroup.end()) {
241 LLVMContext::MD_alias_scope,
243 VersionedInst->
getMetadata(LLVMContext::MD_alias_scope),
244 MDNode::get(Context, GroupToScope[Group->second])));
247 auto NonAliasingScopeList = GroupToNonAliasingScopeList.find(Group->second);
248 if (NonAliasingScopeList != GroupToNonAliasingScopeList.end())
250 LLVMContext::MD_noalias,
252 VersionedInst->
getMetadata(LLVMContext::MD_noalias),
253 NonAliasingScopeList->second));
265 for (
Loop *TopLevelLoop : *LI)
268 if (L->isInnermost())
272 bool Changed =
false;
273 for (
Loop *L : Worklist) {
274 if (!L->isLoopSimplifyForm() || !L->isRotatedForm() ||
275 !L->getExitingBlock())
284 LVer.annotateLoopWithNoAlias();
301 if (
runImpl(&LI, LAIs, &DT, &SE))
static bool runImpl(Function &F, const TargetLowering &TLI)
static cl::opt< bool > AnnotateNoAlias("loop-version-annotate-no-alias", cl::init(true), cl::Hidden, cl::desc("Add no-alias annotation for instructions that " "are disambiguated by memchecks"))
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Analysis pass which computes a DominatorTree.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
This is an important class for using LLVM in a threaded context.
This analysis provides dependence information for the memory accesses of a loop.
const LoopAccessInfo & getInfo(Loop &L)
Drive the analysis of memory accesses in the loop.
const MemoryDepChecker & getDepChecker() const
the Memory Dependence Checker which can determine the loop-independent and loop-carried dependences b...
const RuntimePointerChecking * getRuntimePointerChecking() const
unsigned getNumRuntimePointerChecks() const
Number of memchecks required to prove independence of otherwise may-alias pointers.
const PredicatedScalarEvolution & getPSE() const
Used to add runtime SCEV checks.
bool hasConvergentOp() const
Return true if there is a convergent operation in the loop.
Analysis pass that exposes the LoopInfo for a function.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
This class emits a version of the loop where run-time checks ensure that may-alias pointers can't ove...
void annotateLoopWithNoAlias()
Annotate memory instructions in the versioned loop with no-alias metadata based on the memchecks issu...
void prepareNoAliasMetadata()
Set up the aliasing scopes based on the memchecks.
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 and LoopInfo up...
LoopVersioning(const LoopAccessInfo &LAI, ArrayRef< RuntimePointerCheck > Checks, Loop *L, LoopInfo *LI, DominatorTree *DT, ScalarEvolution *SE)
Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input.
Represents a single loop in the control flow graph.
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
MDNode * createAnonymousAliasScopeDomain(StringRef Name=StringRef())
Return metadata appropriate for an alias scope domain node.
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
const SmallVectorImpl< Instruction * > & getMemoryInstructions() const
The vector of memory access instructions.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
const SCEVPredicate & getPredicate() const
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Holds information about the memory runtime legality checks to verify that a group of pointers do not ...
SmallVector< RuntimeCheckingPtrGroup, 2 > CheckingGroups
Holds a partitioning of pointers into "check groups".
const SmallVectorImpl< RuntimePointerCheck > & getChecks() const
Returns the checks that generateChecks created.
const PointerInfo & getPointerInfo(unsigned PtrIdx) const
Return PointerInfo for pointer at index PtrIdx.
This class uses information about analyze scalars to rewrite expressions in canonical form.
virtual bool isAlwaysTrue() const =0
Returns true if the predicate is always true.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
unsigned getNumOperands() const
iterator find(const KeyT &Val)
LLVM Value Representation.
void setName(const Twine &Name)
Change the name of the value.
iterator_range< user_iterator > users()
StringRef getName() const
Return a constant reference to the value's name.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Value * addRuntimeChecks(Instruction *Loc, Loop *TheLoop, const SmallVectorImpl< RuntimePointerCheck > &PointerChecks, SCEVExpander &Expander, bool HoistRuntimeChecks=false)
Add code that checks at runtime if the accessed arrays in PointerChecks overlap.
Loop * cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, Loop *OrigLoop, ValueToValueMapTy &VMap, const Twine &NameSuffix, LoopInfo *LI, DominatorTree *DT, SmallVectorImpl< BasicBlock * > &Blocks)
Clones a loop OrigLoop.
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
void remapInstructionsInBlocks(ArrayRef< BasicBlock * > Blocks, ValueToValueMapTy &VMap)
Remaps instructions in Blocks using the mapping in VMap.
BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
iterator_range< df_iterator< T > > depth_first(const T &G)
TrackingVH< Value > PointerValue
Holds the pointer value that we need to check.