[LVer] Fix FIXME: hide addPHINodes, NFC
[oota-llvm.git] / include / llvm / Transforms / Utils / LoopVersioning.h
1 //===- LoopVersioning.h - Utility to version a loop -------------*- C++ -*-===//
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 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
17 #define LLVM_TRANSFORMS_UTILS_LOOPVERSIONING_H
18
19 #include "llvm/Transforms/Utils/ValueMapper.h"
20 #include "llvm/Transforms/Utils/LoopUtils.h"
21
22 namespace llvm {
23
24 class Loop;
25 class LoopAccessInfo;
26 class LoopInfo;
27
28 /// \brief This class emits a version of the loop where run-time checks ensure
29 /// that may-alias pointers can't overlap.
30 ///
31 /// It currently only supports single-exit loops and assumes that the loop
32 /// already has a preheader.
33 class LoopVersioning {
34 public:
35   /// \brief Expects MemCheck, LoopAccessInfo, Loop, LoopInfo, DominatorTree
36   /// as input. It uses runtime check provided by user.
37   LoopVersioning(SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks,
38                  const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI,
39                  DominatorTree *DT);
40
41   /// \brief Expects LoopAccessInfo, Loop, LoopInfo, DominatorTree as input.
42   /// It uses default runtime check provided by LoopAccessInfo.
43   LoopVersioning(const LoopAccessInfo &LAInfo, Loop *L, LoopInfo *LI,
44                  DominatorTree *DT);
45
46   /// \brief Performs the CFG manipulation part of versioning the loop including
47   /// the DominatorTree and LoopInfo updates.
48   ///
49   /// The loop that was used to construct the class will be the "versioned" loop
50   /// i.e. the loop that will receive control if all the memchecks pass.
51   ///
52   /// This allows the loop transform pass to operate on the same loop regardless
53   /// of whether versioning was necessary or not:
54   ///
55   ///    for each loop L:
56   ///        analyze L
57   ///        if versioning is necessary version L
58   ///        transform L
59   void versionLoop() { versionLoop(findDefsUsedOutsideOfLoop(VersionedLoop)); }
60
61   /// \brief Same but if the client has already precomputed the set of values
62   /// used outside the loop, this API will allows passing that.
63   void versionLoop(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
64
65   /// \brief Returns the versioned loop.  Control flows here if pointers in the
66   /// loop don't alias (i.e. all memchecks passed).  (This loop is actually the
67   /// same as the original loop that we got constructed with.)
68   Loop *getVersionedLoop() { return VersionedLoop; }
69
70   /// \brief Returns the fall-back loop.  Control flows here if pointers in the
71   /// loop may alias (i.e. one of the memchecks failed).
72   Loop *getNonVersionedLoop() { return NonVersionedLoop; }
73
74 private:
75   /// \brief Adds the necessary PHI nodes for the versioned loops based on the
76   /// loop-defined values used outside of the loop.
77   ///
78   /// This needs to be called after versionLoop if there are defs in the loop
79   /// that are used outside the loop.
80   void addPHINodes(const SmallVectorImpl<Instruction *> &DefsUsedOutside);
81
82   /// \brief The original loop.  This becomes the "versioned" one.  I.e.,
83   /// control flows here if pointers in the loop don't alias.
84   Loop *VersionedLoop;
85   /// \brief The fall-back loop.  I.e. control flows here if pointers in the
86   /// loop may alias (memchecks failed).
87   Loop *NonVersionedLoop;
88
89   /// \brief This maps the instructions from VersionedLoop to their counterpart
90   /// in NonVersionedLoop.
91   ValueToValueMapTy VMap;
92
93   /// \brief The set of checks that we are versioning for.
94   SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks;
95
96   /// \brief Analyses used.
97   const LoopAccessInfo &LAI;
98   LoopInfo *LI;
99   DominatorTree *DT;
100 };
101 }
102
103 #endif