OSDN Git Service

Re-commit my previous SSAUpdater changes. The previous version naively tried
[android-x86/external-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   class BumpPtrAllocator;
25
26 /// SSAUpdater - This class updates SSA form for a set of values defined in
27 /// multiple blocks.  This is used when code duplication or another unstructured
28 /// transformation wants to rewrite a set of uses of one value with uses of a
29 /// set of values.
30 class SSAUpdater {
31 public:
32   class BBInfo;
33   typedef SmallVectorImpl<BBInfo*> BlockListTy;
34
35 private:
36   /// AvailableVals - This keeps track of which value to use on a per-block
37   /// basis.  When we insert PHI nodes, we keep track of them here.
38   //typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
39   void *AV;
40
41   /// PrototypeValue is an arbitrary representative value, which we derive names
42   /// and a type for PHI nodes.
43   Value *PrototypeValue;
44
45   /// BBMap - The GetValueAtEndOfBlock method maintains this mapping from
46   /// basic blocks to BBInfo structures.
47   /// typedef DenseMap<BasicBlock*, BBInfo*> BBMapTy;
48   void *BM;
49
50   /// InsertedPHIs - If this is non-null, the SSAUpdater adds all PHI nodes that
51   /// it creates to the vector.
52   SmallVectorImpl<PHINode*> *InsertedPHIs;
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.  ProtoValue is the value used to name PHI nodes.
61   void Initialize(Value *ProtoValue);
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 private:
104   Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
105   void BuildBlockList(BasicBlock *BB, BlockListTy *BlockList,
106                       BumpPtrAllocator *Allocator);
107   void FindDominators(BlockListTy *BlockList);
108   void FindPHIPlacement(BlockListTy *BlockList);
109   void FindAvailableVals(BlockListTy *BlockList);
110   void FindExistingPHI(BasicBlock *BB, BlockListTy *BlockList);
111   bool CheckIfPHIMatches(PHINode *PHI);
112   void RecordMatchingPHI(PHINode *PHI);
113
114   void operator=(const SSAUpdater&); // DO NOT IMPLEMENT
115   SSAUpdater(const SSAUpdater&);     // DO NOT IMPLEMENT
116 };
117
118 } // End llvm namespace
119
120 #endif