]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/Transforms/Utils/SSAUpdater.h
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / include / llvm / Transforms / Utils / SSAUpdater.h
1 //===-- SSAUpdater.h - Unstructured SSA Update Tool -------------*- 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 declares the SSAUpdater class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
15 #define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
16
17 #include "llvm/ADT/StringRef.h"
18
19 namespace llvm {
20   class BasicBlock;
21   class Instruction;
22   class LoadInst;
23   template<typename T> class SmallVectorImpl;
24   template<typename T> class SSAUpdaterTraits;
25   class PHINode;
26   class Type;
27   class Use;
28   class Value;
29
30 /// SSAUpdater - This class updates SSA form for a set of values defined in
31 /// multiple blocks.  This is used when code duplication or another unstructured
32 /// transformation wants to rewrite a set of uses of one value with uses of a
33 /// set of values.
34 class SSAUpdater {
35   friend class SSAUpdaterTraits<SSAUpdater>;
36
37 private:
38   /// AvailableVals - This keeps track of which value to use on a per-block
39   /// basis.  When we insert PHI nodes, we keep track of them here.
40   //typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
41   void *AV;
42
43   /// ProtoType holds the type of the values being rewritten.
44   Type *ProtoType;
45
46   // PHI nodes are given a name based on ProtoName.
47   std::string ProtoName;
48
49   /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that
50   /// it creates to the vector.
51   SmallVectorImpl<PHINode*> *InsertedPHIs;
52
53 public:
54   /// SSAUpdater constructor.  If InsertedPHIs is specified, it will be filled
55   /// in with all PHI Nodes created by rewriting.
56   explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = 0);
57   ~SSAUpdater();
58
59   /// Initialize - Reset this object to get ready for a new set of SSA
60   /// updates with type 'Ty'.  PHI nodes get a name based on 'Name'.
61   void Initialize(Type *Ty, StringRef Name);
62
63   /// AddAvailableValue - Indicate that a rewritten value is available at the
64   /// end of the specified block with the specified value.
65   void AddAvailableValue(BasicBlock *BB, Value *V);
66
67   /// HasValueForBlock - Return true if the SSAUpdater already has a value for
68   /// the specified block.
69   bool HasValueForBlock(BasicBlock *BB) const;
70
71   /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
72   /// live at the end of the specified block.
73   Value *GetValueAtEndOfBlock(BasicBlock *BB);
74
75   /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
76   /// is live in the middle of the specified block.
77   ///
78   /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
79   /// important case: if there is a definition of the rewritten value after the
80   /// 'use' in BB.  Consider code like this:
81   ///
82   ///      X1 = ...
83   ///   SomeBB:
84   ///      use(X)
85   ///      X2 = ...
86   ///      br Cond, SomeBB, OutBB
87   ///
88   /// In this case, there are two values (X1 and X2) added to the AvailableVals
89   /// set by the client of the rewriter, and those values are both live out of
90   /// their respective blocks.  However, the use of X happens in the *middle* of
91   /// a block.  Because of this, we need to insert a new PHI node in SomeBB to
92   /// merge the appropriate values, and this value isn't live out of the block.
93   ///
94   Value *GetValueInMiddleOfBlock(BasicBlock *BB);
95
96   /// RewriteUse - Rewrite a use of the symbolic value.  This handles PHI nodes,
97   /// which use their value in the corresponding predecessor.  Note that this
98   /// will not work if the use is supposed to be rewritten to a value defined in
99   /// the same block as the use, but above it.  Any 'AddAvailableValue's added
100   /// for the use's block will be considered to be below it.
101   void RewriteUse(Use &U);
102
103   /// RewriteUseAfterInsertions - Rewrite a use, just like RewriteUse.  However,
104   /// this version of the method can rewrite uses in the same block as a
105   /// definition, because it assumes that all uses of a value are below any
106   /// inserted values.
107   void RewriteUseAfterInsertions(Use &U);
108
109 private:
110   Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
111
112   void operator=(const SSAUpdater&) LLVM_DELETED_FUNCTION;
113   SSAUpdater(const SSAUpdater&) LLVM_DELETED_FUNCTION;
114 };
115   
116 /// LoadAndStorePromoter - This little helper class provides a convenient way to
117 /// promote a collection of loads and stores into SSA Form using the SSAUpdater.
118 /// This handles complexities that SSAUpdater doesn't, such as multiple loads
119 /// and stores in one block.
120 ///
121 /// Clients of this class are expected to subclass this and implement the
122 /// virtual methods.
123 ///
124 class LoadAndStorePromoter {
125 protected:
126   SSAUpdater &SSA;
127 public:
128   LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts,
129                        SSAUpdater &S, StringRef Name = StringRef());
130   virtual ~LoadAndStorePromoter() {}
131   
132   /// run - This does the promotion.  Insts is a list of loads and stores to
133   /// promote, and Name is the basename for the PHIs to insert.  After this is
134   /// complete, the loads and stores are removed from the code.
135   void run(const SmallVectorImpl<Instruction*> &Insts) const;
136   
137   
138   /// Return true if the specified instruction is in the Inst list (which was
139   /// passed into the run method).  Clients should implement this with a more
140   /// efficient version if possible.
141   virtual bool isInstInList(Instruction *I,
142                             const SmallVectorImpl<Instruction*> &Insts) const;
143   
144   /// doExtraRewritesBeforeFinalDeletion - This hook is invoked after all the
145   /// stores are found and inserted as available values, but 
146   virtual void doExtraRewritesBeforeFinalDeletion() const {
147   }
148   
149   /// replaceLoadWithValue - Clients can choose to implement this to get
150   /// notified right before a load is RAUW'd another value.
151   virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {
152   }
153
154   /// This is called before each instruction is deleted.
155   virtual void instructionDeleted(Instruction *I) const {
156   }
157
158   /// updateDebugInfo - This is called to update debug info associated with the
159   /// instruction.
160   virtual void updateDebugInfo(Instruction *I) const {
161   }
162 };
163
164 } // End llvm namespace
165
166 #endif