1 //===- LoopVersioning.h - Utility to version a loop -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a utility class to perform loop versioning. The versioned
11 // loop speculates that otherwise may-aliasing memory accesses don't overlap and
12 // emits checks to prove this.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
17 #define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
19 #include "llvm/Transforms/Utils/ValueMapper.h"
27 /// \brief This class emits a version of the loop where run-time checks ensure
28 /// that may-alias pointers can't overlap.
30 /// It currently only supports single-exit loops and assumes that the loop
31 /// already has a preheader.
32 class LoopVersioning {
34 LoopVersioning(SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks,
35 const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI,
37 const SmallVector<int, 8> *PtrToPartition = nullptr);
39 /// \brief Performs the CFG manipulation part of versioning the loop including
40 /// the DominatorTree and LoopInfo updates.
42 /// The loop that was used to construct the class will be the "versioned" loop
43 /// i.e. the loop that will receive control if all the memchecks pass.
45 /// This allows the loop transform pass to operate on the same loop regardless
46 /// of whether versioning was necessary or not:
50 /// if versioning is necessary version L
52 void versionLoop(Pass *P);
54 /// \brief Adds the necessary PHI nodes for the versioned loops based on the
55 /// loop-defined values used outside of the loop.
57 /// This needs to be called after versionLoop if there are defs in the loop
58 /// that are used outside the loop. FIXME: this should be invoked internally
59 /// by versionLoop and made private.
60 void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
62 /// \brief Returns the versioned loop. Control flows here if pointers in the
63 /// loop don't alias (i.e. all memchecks passed). (This loop is actually the
64 /// same as the original loop that we got constructed with.)
65 Loop *getVersionedLoop() { return VersionedLoop; }
67 /// \brief Returns the fall-back loop. Control flows here if pointers in the
68 /// loop may alias (i.e. one of the memchecks failed).
69 Loop *getNonVersionedLoop() { return NonVersionedLoop; }
72 /// \brief The original loop. This becomes the "versioned" one. I.e.,
73 /// control flows here if pointers in the loop don't alias.
75 /// \brief The fall-back loop. I.e. control flows here if pointers in the
76 /// loop may alias (memchecks failed).
77 Loop *NonVersionedLoop;
79 /// \brief This maps the instructions from VersionedLoop to their counterpart
80 /// in NonVersionedLoop.
81 ValueToValueMapTy VMap;
83 /// \brief The set of checks that we are versioning for.
84 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks;
86 /// \brief Analyses used.
87 const LoopAccessInfo &LAI;