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