Make RecursivelyDeleteTriviallyDeadInstructions,
[oota-llvm.git] / 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 namespace llvm {
18   class Value;
19   class BasicBlock;
20   class Use;
21   class PHINode;
22   template<typename T>
23   class SmallVectorImpl;
24
25 /// SSAUpdater - This class updates SSA form for a set of values defined in
26 /// multiple blocks.  This is used when code duplication or another unstructured
27 /// transformation wants to rewrite a set of uses of one value with uses of a
28 /// set of values.
29 class SSAUpdater {
30   /// AvailableVals - This keeps track of which value to use on a per-block
31   /// basis.  When we insert PHI nodes, we keep track of them here.  We use
32   /// TrackingVH's for the value of the map because we RAUW PHI nodes when we
33   /// eliminate them, and want the TrackingVH's to track this.
34   //typedef DenseMap<BasicBlock*, TrackingVH<Value> > AvailableValsTy;
35   void *AV;
36
37   /// PrototypeValue is an arbitrary representative value, which we derive names
38   /// and a type for PHI nodes.
39   Value *PrototypeValue;
40
41   /// IncomingPredInfo - We use this as scratch space when doing our recursive
42   /// walk.  This should only be used in GetValueInBlockInternal, normally it
43   /// should be empty.
44   //std::vector<std::pair<BasicBlock*, TrackingVH<Value> > > IncomingPredInfo;
45   void *IPI;
46
47   /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that
48   /// it creates to the vector.
49   SmallVectorImpl<PHINode*> *InsertedPHIs;
50 public:
51   /// SSAUpdater constructor.  If InsertedPHIs is specified, it will be filled
52   /// in with all PHI Nodes created by rewriting.
53   explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = 0);
54   ~SSAUpdater();
55
56   /// Initialize - Reset this object to get ready for a new set of SSA
57   /// updates.  ProtoValue is the value used to name PHI nodes.
58   void Initialize(Value *ProtoValue);
59
60   /// AddAvailableValue - Indicate that a rewritten value is available at the
61   /// end of the specified block with the specified value.
62   void AddAvailableValue(BasicBlock *BB, Value *V);
63
64   /// HasValueForBlock - Return true if the SSAUpdater already has a value for
65   /// the specified block.
66   bool HasValueForBlock(BasicBlock *BB) const;
67
68   /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
69   /// live at the end of the specified block.
70   Value *GetValueAtEndOfBlock(BasicBlock *BB);
71
72   /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
73   /// is live in the middle of the specified block.
74   ///
75   /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
76   /// important case: if there is a definition of the rewritten value after the
77   /// 'use' in BB.  Consider code like this:
78   ///
79   ///      X1 = ...
80   ///   SomeBB:
81   ///      use(X)
82   ///      X2 = ...
83   ///      br Cond, SomeBB, OutBB
84   ///
85   /// In this case, there are two values (X1 and X2) added to the AvailableVals
86   /// set by the client of the rewriter, and those values are both live out of
87   /// their respective blocks.  However, the use of X happens in the *middle* of
88   /// a block.  Because of this, we need to insert a new PHI node in SomeBB to
89   /// merge the appropriate values, and this value isn't live out of the block.
90   ///
91   Value *GetValueInMiddleOfBlock(BasicBlock *BB);
92
93   /// RewriteUse - Rewrite a use of the symbolic value.  This handles PHI nodes,
94   /// which use their value in the corresponding predecessor.  Note that this
95   /// will not work if the use is supposed to be rewritten to a value defined in
96   /// the same block as the use, but above it.  Any 'AddAvailableValue's added
97   /// for the use's block will be considered to be below it.
98   void RewriteUse(Use &U);
99
100 private:
101   Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
102   void operator=(const SSAUpdater&); // DO NOT IMPLEMENT
103   SSAUpdater(const SSAUpdater&);     // DO NOT IMPLEMENT
104 };
105
106 } // End llvm namespace
107
108 #endif