add a helper function.
[oota-llvm.git] / include / llvm / Transforms / Utils / Local.h
1 //===-- Local.h - Functions to perform local transformations ----*- 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 family of functions perform various local transformations to the
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
16 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
17
18 namespace llvm {
19
20 class User;
21 class BasicBlock;
22 class BranchInst;
23 class Instruction;
24 class Value;
25 class Pass;
26 class PHINode;
27 class AllocaInst;
28 class ConstantExpr;
29 class TargetData;
30 class DbgInfoIntrinsic;
31
32 template<typename T> class SmallVectorImpl;
33   
34 //===----------------------------------------------------------------------===//
35 //  Local analysis.
36 //
37
38 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
39 /// from this value cannot trap.  If it is not obviously safe to load from the
40 /// specified pointer, we do a quick local scan of the basic block containing
41 /// ScanFrom, to determine if the address is already accessed.
42 bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom);
43
44 //===----------------------------------------------------------------------===//
45 //  Local constant propagation.
46 //
47
48 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
49 /// constant value, convert it into an unconditional branch to the constant
50 /// destination.  This is a nontrivial operation because the successors of this
51 /// basic block must have their PHI nodes updated.
52 ///
53 bool ConstantFoldTerminator(BasicBlock *BB);
54
55 //===----------------------------------------------------------------------===//
56 //  Local dead code elimination.
57 //
58
59 /// isInstructionTriviallyDead - Return true if the result produced by the
60 /// instruction is not used, and the instruction has no side effects.
61 ///
62 bool isInstructionTriviallyDead(Instruction *I);
63
64 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
65 /// trivially dead instruction, delete it.  If that makes any of its operands
66 /// trivially dead, delete them too, recursively.  Return true if any
67 /// instructions were deleted.
68 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
69
70 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
71 /// dead PHI node, due to being a def-use chain of single-use nodes that
72 /// either forms a cycle or is terminated by a trivially dead instruction,
73 /// delete it.  If that makes any of its operands trivially dead, delete them
74 /// too, recursively.  Return true if the PHI node is actually deleted.
75 bool RecursivelyDeleteDeadPHINode(PHINode *PN);
76
77   
78 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
79 /// simplify any instructions in it and recursively delete dead instructions.
80 ///
81 /// This returns true if it changed the code, note that it can delete
82 /// instructions in other blocks as well in this block.
83 bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
84     
85 //===----------------------------------------------------------------------===//
86 //  Control Flow Graph Restructuring.
87 //
88
89 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
90 /// method is called when we're about to delete Pred as a predecessor of BB.  If
91 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
92 ///
93 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
94 /// nodes that collapse into identity values.  For example, if we have:
95 ///   x = phi(1, 0, 0, 0)
96 ///   y = and x, z
97 ///
98 /// .. and delete the predecessor corresponding to the '1', this will attempt to
99 /// recursively fold the 'and' to 0.
100 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
101                                   TargetData *TD = 0);
102     
103   
104 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
105 /// predecessor is known to have one successor (BB!).  Eliminate the edge
106 /// between them, moving the instructions in the predecessor into BB.  This
107 /// deletes the predecessor block.
108 ///
109 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
110     
111
112 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
113 /// unconditional branch, and contains no instructions other than PHI nodes,
114 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
115 /// rewriting all the predecessors to branch to the successor block and return
116 /// true.  If we can't transform, return false.
117 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
118
119 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
120 /// nodes in this block. This doesn't try to be clever about PHI nodes
121 /// which differ only in the order of the incoming values, but instcombine
122 /// orders them so it usually won't matter.
123 ///
124 bool EliminateDuplicatePHINodes(BasicBlock *BB);
125
126 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
127 /// example, it adjusts branches to branches to eliminate the extra hop, it
128 /// eliminates unreachable basic blocks, and does other "peephole" optimization
129 /// of the CFG.  It returns true if a modification was made, possibly deleting
130 /// the basic block that was pointed to.
131 ///
132 /// WARNING:  The entry node of a method may not be simplified.
133 ///
134 bool SimplifyCFG(BasicBlock *BB);
135
136 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
137 /// and if a predecessor branches to us and one of our successors, fold the
138 /// setcc into the predecessor and use logical operations to pick the right
139 /// destination.
140 bool FoldBranchToCommonDest(BranchInst *BI);
141
142 /// DemoteRegToStack - This function takes a virtual register computed by an
143 /// Instruction and replaces it with a slot in the stack frame, allocated via
144 /// alloca.  This allows the CFG to be changed around without fear of
145 /// invalidating the SSA information for the value.  It returns the pointer to
146 /// the alloca inserted to create a stack slot for X.
147 ///
148 AllocaInst *DemoteRegToStack(Instruction &X,
149                              bool VolatileLoads = false,
150                              Instruction *AllocaPoint = 0);
151
152 /// DemotePHIToStack - This function takes a virtual register computed by a phi
153 /// node and replaces it with a slot in the stack frame, allocated via alloca.
154 /// The phi node is deleted and it returns the pointer to the alloca inserted. 
155 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
156
157 /// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used
158 /// by DbgIntrinsics. If DbgInUses is specified then the vector is filled 
159 /// with DbgInfoIntrinsic that use the instruction I.
160 bool OnlyUsedByDbgInfoIntrinsics(Instruction *I, 
161                            SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses = 0);
162
163 } // End llvm namespace
164
165 #endif