If an AllocaInst referred by DbgDeclareInst is used by a LoadInst then the LoadInst...
[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 Function;
23 class BranchInst;
24 class Instruction;
25 class DbgDeclareInst;
26 class StoreInst;
27 class LoadInst;
28 class Value;
29 class Pass;
30 class PHINode;
31 class AllocaInst;
32 class ConstantExpr;
33 class TargetData;
34 class DIBuilder;
35
36 template<typename T> class SmallVectorImpl;
37   
38 //===----------------------------------------------------------------------===//
39 //  Local constant propagation.
40 //
41
42 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
43 /// constant value, convert it into an unconditional branch to the constant
44 /// destination.  This is a nontrivial operation because the successors of this
45 /// basic block must have their PHI nodes updated.
46 ///
47 bool ConstantFoldTerminator(BasicBlock *BB);
48
49 //===----------------------------------------------------------------------===//
50 //  Local dead code elimination.
51 //
52
53 /// isInstructionTriviallyDead - Return true if the result produced by the
54 /// instruction is not used, and the instruction has no side effects.
55 ///
56 bool isInstructionTriviallyDead(Instruction *I);
57
58 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
59 /// trivially dead instruction, delete it.  If that makes any of its operands
60 /// trivially dead, delete them too, recursively.  Return true if any
61 /// instructions were deleted.
62 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
63
64 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
65 /// dead PHI node, due to being a def-use chain of single-use nodes that
66 /// either forms a cycle or is terminated by a trivially dead instruction,
67 /// delete it.  If that makes any of its operands trivially dead, delete them
68 /// too, recursively.  Return true if a change was made.
69 bool RecursivelyDeleteDeadPHINode(PHINode *PN);
70
71   
72 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
73 /// simplify any instructions in it and recursively delete dead instructions.
74 ///
75 /// This returns true if it changed the code, note that it can delete
76 /// instructions in other blocks as well in this block.
77 ///
78 /// WARNING: Do not use this function on unreachable blocks, as recursive
79 /// simplification is not able to handle corner-case scenarios that can
80 /// arise in them.
81 bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
82     
83 //===----------------------------------------------------------------------===//
84 //  Control Flow Graph Restructuring.
85 //
86
87 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
88 /// method is called when we're about to delete Pred as a predecessor of BB.  If
89 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
90 ///
91 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
92 /// nodes that collapse into identity values.  For example, if we have:
93 ///   x = phi(1, 0, 0, 0)
94 ///   y = and x, z
95 ///
96 /// .. and delete the predecessor corresponding to the '1', this will attempt to
97 /// recursively fold the 'and' to 0.
98 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
99                                   TargetData *TD = 0);
100     
101   
102 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
103 /// predecessor is known to have one successor (BB!).  Eliminate the edge
104 /// between them, moving the instructions in the predecessor into BB.  This
105 /// deletes the predecessor block.
106 ///
107 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
108     
109
110 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
111 /// unconditional branch, and contains no instructions other than PHI nodes,
112 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
113 /// rewriting all the predecessors to branch to the successor block and return
114 /// true.  If we can't transform, return false.
115 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
116
117 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
118 /// nodes in this block. This doesn't try to be clever about PHI nodes
119 /// which differ only in the order of the incoming values, but instcombine
120 /// orders them so it usually won't matter.
121 ///
122 bool EliminateDuplicatePHINodes(BasicBlock *BB);
123
124 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
125 /// example, it adjusts branches to branches to eliminate the extra hop, it
126 /// eliminates unreachable basic blocks, and does other "peephole" optimization
127 /// of the CFG.  It returns true if a modification was made, possibly deleting
128 /// the basic block that was pointed to.
129 ///
130 bool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0);
131
132 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
133 /// and if a predecessor branches to us and one of our successors, fold the
134 /// setcc into the predecessor and use logical operations to pick the right
135 /// destination.
136 bool FoldBranchToCommonDest(BranchInst *BI);
137
138 /// DemoteRegToStack - This function takes a virtual register computed by an
139 /// Instruction and replaces it with a slot in the stack frame, allocated via
140 /// alloca.  This allows the CFG to be changed around without fear of
141 /// invalidating the SSA information for the value.  It returns the pointer to
142 /// the alloca inserted to create a stack slot for X.
143 ///
144 AllocaInst *DemoteRegToStack(Instruction &X,
145                              bool VolatileLoads = false,
146                              Instruction *AllocaPoint = 0);
147
148 /// DemotePHIToStack - This function takes a virtual register computed by a phi
149 /// node and replaces it with a slot in the stack frame, allocated via alloca.
150 /// The phi node is deleted and it returns the pointer to the alloca inserted. 
151 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
152
153 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
154 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
155 /// and it is more than the alignment of the ultimate object, see if we can
156 /// increase the alignment of the ultimate object, making this check succeed.
157 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
158                                     const TargetData *TD = 0);
159
160 /// getKnownAlignment - Try to infer an alignment for the specified pointer.
161 static inline unsigned getKnownAlignment(Value *V, const TargetData *TD = 0) {
162   return getOrEnforceKnownAlignment(V, 0, TD);
163 }
164
165 ///===---------------------------------------------------------------------===//
166 ///  Dbg Intrinsic utilities
167 ///
168
169 /// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value
170 /// that has an associated llvm.dbg.decl intrinsic.
171 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
172                                      StoreInst *SI, DIBuilder &Builder);
173
174 /// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value
175 /// that has an associated llvm.dbg.decl intrinsic.
176 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
177                                      LoadInst *LI, DIBuilder &Builder);
178
179 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
180 /// of llvm.dbg.value intrinsics.
181 bool LowerDbgDeclare(Function &F);
182
183 } // End llvm namespace
184
185 #endif