First try at implementing the AliasSetTracker class. I'm sure it will need
[oota-llvm.git] / include / llvm / Analysis / AliasSetTracker.h
1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
2 //
3 // This file defines two classes: AliasSetTracker and AliasSet.  These interface
4 // are used to classify a collection of pointer references into a maximal number
5 // of disjoint sets.  Each AliasSet object constructed by the AliasSetTracker
6 // object refers to memory disjoint from the other sets.
7 // 
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
11 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
12
13 #include <vector>
14 class AliasAnalysis;
15 class LoadInst;
16 class StoreInst;
17 class CallInst;
18 class InvokeInst;
19 class Value;
20 class AliasSetTracker;
21
22 class AliasSet {
23   friend class AliasSetTracker;
24   std::vector<LoadInst*> Loads;
25   std::vector<StoreInst*> Stores;
26   std::vector<CallInst*> Calls;
27   std::vector<InvokeInst*> Invokes;
28 public:
29   /// AccessType - Keep track of whether this alias set merely refers to the
30   /// locations of memory, whether it modifies the memory, or whether it does
31   /// both.  The lattice goes from "None" (alias set not present) to either Refs
32   /// or Mods, then to ModRef as neccesary.
33   ///
34   enum AccessType {
35     Refs, Mods, ModRef
36   };
37
38   /// AliasType - Keep track the relationships between the pointers in the set.
39   /// Lattice goes from MustAlias to MayAlias.
40   ///
41   enum AliasType {
42     MustAlias, MayAlias
43   };
44 private:
45   enum AccessType AccessTy;
46   enum AliasType  AliasTy;
47 public:
48   /// Accessors...
49   enum AccessType getAccessType() const { return AccessTy; }
50   enum AliasType  getAliasType()  const { return AliasTy; }
51
52   // TODO: in the future, add a fixed size (4? 2?) cache of pointers that we
53   // know are in the alias set, to cut down time answering "pointeraliasesset"
54   // queries.
55
56   /// pointerAliasesSet - Return true if the specified pointer "may" (or must)
57   /// alias one of the members in the set.
58   ///
59   bool pointerAliasesSet(const Value *Ptr, AliasAnalysis &AA) const;
60
61   /// mergeSetIn - Merge the specified alias set into this alias set...
62   ///
63   void mergeSetIn(const AliasSet &AS);
64
65   const std::vector<LoadInst*>   &getLoads()   const { return Loads; }
66   const std::vector<StoreInst*>  &getStores()  const { return Stores; }
67   const std::vector<CallInst*>   &getCalls()   const { return Calls; }
68   const std::vector<InvokeInst*> &getInvokes() const { return Invokes; }
69
70 private:
71   AliasSet() : AliasTy(MustAlias) {} // Can only be created by AliasSetTracker
72   void updateAccessType();
73   Value *getSomePointer() const;
74 };
75
76
77 class AliasSetTracker {
78   AliasAnalysis &AA;
79   std::vector<AliasSet> AliasSets;
80 public:
81   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
82   /// the specified alias analysis object to disambiguate load and store
83   /// addresses.
84   AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
85
86
87   /// add methods - These methods are used to add different types of
88   /// instructions to the alias sets.  Adding a new instruction can result in
89   /// one of three actions happening:
90   ///
91   ///   1. If the instruction doesn't alias any other sets, create a new set.
92   ///   2. If the instruction aliases exactly one set, add it to the set
93   ///   3. If the instruction aliases multiple sets, merge the sets, and add
94   ///      the instruction to the result.
95   ///
96   void add(LoadInst *LI);
97   void add(StoreInst *SI);
98   void add(CallInst *CI);
99   void add(InvokeInst *II);
100
101   /// getAliasSets - Return the alias sets that are active.
102   const std::vector<AliasSet> &getAliasSets() const { return AliasSets; }
103
104 private:
105   AliasSet *findAliasSetForPointer(const Value *Ptr);
106   void mergeAllSets();
107 };
108
109 #endif